1

I'm trying to build a function, that gets a string of letters, and prints the amount of each letter in the string. for example: input: String = "aaabbaccxyxyx" output: 4a2b2c3x2y

This is what I've come up with:

public class Q1 {
    public static String numLetters(String s){
        String end = new String();
        int counter = 0;
        char c,d;
        for(int i=0; i<s.length();i++){
            c = s.charAt(i);
            for(int j=0; j<s.length();j++){
                d = s.charAt(j);
                if(c == d){
                    counter++;
                }
            }
            end = end + counter+c;
            counter = 0;
        }

        return end;
    }

but, this is the output: 4a4a4a2b2b4a2c2c3x2y3x2y3x A lot of repeats..

Any help how to make it right? Keep in mind, the function needs to return a string, not just prints it out. Thanks! =)

  • 1
    Possible duplicate of [How to count frequency of characters in a string?](http://stackoverflow.com/questions/6712587/how-to-count-frequency-of-characters-in-a-string) – Laszlo Hirdi Feb 07 '17 at 22:24
  • It's not a duplicate, the function needs to return a string, not just prints it out – Itay Ben Moshe Feb 07 '17 at 22:41

4 Answers4

1

I would make an int array to keep the count of each letter in in the string. Because there are 26 letters, the length of the array should be 26:

public static String numLetters(String s) {
    int[] count = new int[26];
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        count[(int)(c - 'a')]++;
    }
    String ans = "";
    for (int i = 0; i < 26; i++) {
        if (count[i] != 0) {
            ans += String.valueOf(count[i]) + (char)(i + 'a');
        }
    }
    return ans;
}
4castle
  • 32,613
  • 11
  • 69
  • 106
Mohsen_Fatemi
  • 2,183
  • 2
  • 16
  • 25
  • that could be useful, but look at the head "public static String", the function needs to return a string. – Itay Ben Moshe Feb 07 '17 at 22:36
  • @ItayBenMoshe i'll edit my answer now , mark it as true if you find it useful – Mohsen_Fatemi Feb 07 '17 at 22:38
  • great job! You forgot to creat a c 'char' variable, but everything else just great! Thanks! :) – Itay Ben Moshe Feb 07 '17 at 23:15
  • You should use a `StringBuilder` for `ans`, so that the last loop will be more efficient. String concatenation requires making a copy of the string into a new array, and the compiler can't optimize that when it's in a loop. – 4castle Feb 08 '17 at 01:12
1

A straightforward variant could look like this:

public static String countChars(String arg) {
    String res = "";
    boolean[] counted = new boolean[arg.length()];
    for (int i = 0; i < counted.length; i++) {
        if (!counted[i]) {
            char c = arg.charAt(i);
            int counter = 1;
            for (int j = i + 1; j < counted.length; j++) {
                if (arg.charAt(j) == c) {
                    counter++;
                    counted[j] = true;
                }
            }
            res += counter + "" + c;
        }
    }
    return res;
}
tierriminator
  • 587
  • 5
  • 19
1

If you want to keep your original structure, I suggest using a StringBuilder so that you can delete characters that you have already seen. In case you delete a character, you have to adjust your indexes i and j.

public static String numLetters(String str){
    StringBuilder s = new StringBuilder(s);
    String end = new String();
    int counter = 0;
    char c,d;
    for(int i=0; i<s.length();i++){
        c = s.charAt(i);
        for(int j=0; j<s.length();j++){
            d = s.charAt(j);
            if(c == d){
                s.deleteCharAt(j);
                if (i >= j) i--;
                j--;
                counter++;
            }
        }
        end = end + counter+c;
        counter = 0;
    }

    return end;
}
0

Try this:

int count = StringUtils.countMatches("a.b.c.d", ".");
user3272686
  • 853
  • 2
  • 11
  • 23