pythonjavastringcharacter

How to count and compare characters in a string with Python/Java


Once I was invited for an interview for a Junior Java Developer role. Everything was ok, until the technical interview... It was a live code challenge, where the tech leads would watch me solve a code challenge in Java.

It was about a year ago, an until now, I don't know how to solve it... I've studying Python since then, and even tried to solve it in this language, but, with no results...

The challenge is this:

Based on a string input from the user, write a function that returns how many characters you should add so the string is "evenly" distributed.

Example 1:

The input was "abba". This string has 3 groups: First: "a" Second: "bb" Third: "a"

So in order to "evenly" balance this string, we should do the following: In the first group, we should add 1 character "a" In the second group, we should do nothing.. In the third group, we should add 1 character "a"

So, doing this, the string would be like this: "aabbaa" And, the return value of the function would be "2"

Example 2:

The input was "baaababba". This string has 6 groups: First: "b" Second: "aaa" Third: "b" Fourth: "a" Fifth: "bb" Sixth: "a"

So in order to "evenly" balance this string, we should do the following: In the first group, we should add 2 characters "b" In the second group, we should do nothing.. In the third group, we should add 2 character "b" In the fourth group, we should add 2 characters "a" In the fifth group, we should add 1 character "b" In the sixth group, we should add 2 characters "a"

So, doing this, the string would be like this: "bbbaaabbbaaabbbaaa" And, the return value of the function would be "9"

Example 3:

The input was "aaaa". This string has 1 group: "aaaa"

So, there is no need to add characters to even this string, and the return value should be "0"

Example 4:

The input was "aabbaabb". This string has 4 groups: First group: "aa" Second group: "bb" Third group: "aa" Fourth group: "bb"

There is no need to add any characters to balance this string, since all groups have the same number of characters. Then, for this one, the return should, as well, be "0".

Please, if you could help me, both in Java or Python, I'll really appreciate it. This challenge is driving me crazy since then.

BTW... I am new in the world of programming.

def string_evaluator(user_input):
    input_to_list = []
    list_to_dic = {}
    group_count = 1
    biggest_group = 1
    for i in user_input:
        input_to_list.append(i)
    for char in input_to_list:
        character = char[0]
        if character not in list_to_dic:
        list_to_dic[character] = []
        list_to_dic[character].append(char)

    # print(list_to_dic)

Solution

  • Essentially, this problem requires finding all consecutive sequences of the same character. Then, we just need to sum the difference between the length of each group and the length of the largest group.

    In Python, we can use itertools.groupby to get all consecutive runs of the same character. A straightforward implementation would be as follows:

    from itertools import groupby
    def balance(s):
        groups = [len(list(g)) for k, g in groupby(s)]
        maxLen = max(groups)
        return sum(maxLen - x for x in groups)