a’s and b’s: Devising An Algorithm For A Pattern of Chars

Within the little free time that I have (geez, I cannot WAIT to get a programming job), I’ve been working out an algorithm for the puzzle from the interview I spoke about on the 25th of Aug. Here’s a reminder of the puzzle:

A String S will be comprised of a series of letters of only ‘a’ and ‘b’ that is of length N. For any N of S, write a function that will make the amount of groups of letters equivalent. As an example, if the input was ‘aababa’, the algorithm would output ‘aabbaabbaa’ thus making each group equal with each other.

Here is my thought process:
1. Since there is no dialogue witht he interviewer, I will assume that the string of a’s and b’s can be the max length of an integer data type.
2. I will create an array of some kind and have the program scan the first series of letters that are either a or b (I will dub the series as “groups”)
3. during iteration, I will add each upcoming a group and upcoming b group to separate array objects arrA and arrB.
4. then compare which of arrA and arrB is the longest and store that number in an int

uh oh, break is over. More on this later!

p.s. here is my code so far:

static ArrayList<Character> Pattern(ArrayList<Character> s){
    ArrayList proxy = new ArrayList<>();
    char a = 'a';
    char b;

    //create two string obj: one to store the length of the longest char group, other to take a group and compare
    ArrayList<Character> arrA = new ArrayList<Character>();
    ArrayList<Character> arrB = new ArrayList<Character>();
    int compare = 0;
    //need to iterate over entire list and find largets group
    for (int i = 0; i < s.size(); i++){
        if (s.get(i) == 'a'){
            arrA.add(s.get(i));
        }else{
            break;
        }
        if (s.get(i) == 'b'){
            arrB.add(s.get(i));
        }else{
            break;
        }
        if (arrA.size() > arrB.size()){
            compare = arrA.size();
        }else{
            compare = arrB.size();
        }
    }

    return proxy;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: