Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn String aaaabbbbddd into a4b4d3

Tags:

java

string

I'm trying to get a head start on practicing interview questions and I came across this one:

Turn String aaaabbbbddd into a4b4d3

You would basically want to convert the existing string into a string with each unique character occurrence and the number of times the character occurs.

This is my solution but I think it could be refined into something more elegant:

    String s = "aaaabbbbddd";
    String modified = "";
    int len = s.length();
    char[] c = s.toCharArray();
    int count = 0;
    for (int i = 0; i < len; i++) {
        count = 1;
        for (int j = i + 1; j < len; j++) {
            if (c[i] == ' ') {
                break;
            }
            if (c[i] == c[j]) {
                count++;
                c[j] = ' ';
            }

        }
        if (c[i] != ' ') {
            modified += c[i] + "" +  count; 

        }
    }
    System.out.println(modified);

Does anyone have any other suggestions for a solution?

like image 947
user3029486 Avatar asked Dec 01 '13 05:12

user3029486


3 Answers

Employ a Map<Character, Integer> instead. Attempt to insert the new character into the map; if it already exists, then increment the value for that particular character.

Example:

Map<Character, Integer> countMap = new HashMap<>();
if(!countMap.containsKey('a')) {
    countMap.put('a', 1);
} else {
    countMap.put('a', countMap.get('a') + 1);
}
like image 123
Makoto Avatar answered Oct 21 '22 17:10

Makoto


To add on to @Makoto's wonderful answer, in your situation, I would use a TreeMap instead of a HashMap. A TreeMap will allow you to print in alphabetical order. I have also added the print code to show you how it would look. It's fully runnable.

import java.util.Map;
import java.util.TreeMap;

public class MapPractice {

    public static void main(String[] args) {
        Map<Character, Integer> map = new TreeMap<>();

        String blah = "aaaabbbbddd";

        for (int i = 0; i < blah.length(); i++) {
            char c = blah.charAt(i);
            if (!map.containsKey(c)) {
                map.put(c, 1);
            } else {
                map.put(c, (map.get(c) + 1));
            }
        } 

        for (Map.Entry<Character, Integer> entry: map.entrySet()) {
            System.out.print(entry.getKey() + "" + entry.getValue());
        }
    }
}

Output with TreeMap: a4b4d3

Output with HashMap: d3b4a4

like image 3
Paul Samsotha Avatar answered Oct 21 '22 17:10

Paul Samsotha


My version

    StringBuilder sb = new StringBuilder();
    int count = 0;
    char last = s.charAt(0);
    for(char c : s.toCharArray()) {
        if (c == last) {
            count++;
        } else {
            sb.append(last).append(count);
            count = 0;
            last = c;
        }
    }
    if (count != 0) {
        sb.append(last).append(count);
    }
    System.out.println(sb);
like image 1
Evgeniy Dorofeev Avatar answered Oct 21 '22 18:10

Evgeniy Dorofeev