Question: I need to find an algorithm which counts the letters in any string (e.g. "cabababb") and the output must be in a alphabetical manner(e.g. a: 3, b: 4, c:1 etc.) . There is no need to distinguish between upper and lower case letters. I am not allowed to youse a HashMap.

This is my code till now: I'ts not working, I think I've got the wrong strategy here. Can you help me please?

```
public class Stringray extends MiniJava{
public static void main(String[] args) {
    // Texteingabe
    String input = readString("Geben Sie einen Text ein: ");

    String output = "";
    int i = 0;

    // Häufigkeit der Buchstaben
    int count = 0;
    char letter = 'a';
    while(i < input.length()) {
            while(i < input.length()) {
                if(input.charAt(i) == letter) {
                    while(i < input.length()) {
                        count++;
                        i++;
                    }
                    output = output + letter + ": " + count + "\t";
                }
                i++;
            }
            letter++;
    }
    write(output);
}
}

```

Answer: 
You can solve the problem by following the steps below like QBrute correctly pointed out.

```
public String countMatches(String main) {
    //Create an array of the alphabets length
    main = main.toLowerCase();
    int[] foundArray = new int[26];
    String output = "";
    //Create an alphabets array
    String[] alphabets = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split("\\s");
    //Split the main String array
    String[] mainArray = main.split("");
    for (String string : mainArray) {
        //Iterate through the array
        for (int i = 0; i < alphabets.length; i++) {
            //If the string index matches any of our alphabets
            if (string.equals(alphabets[i])) {
                //Increase the corresponding foundArray index value
                foundArray[i] = foundArray[i] + 1;
            }
        }
    }
    //Iterate through the foundArray
    for (int i = 0; i < foundArray.length; i++) {
        //If an index is greater than 0
        if (foundArray[i] > 0) {
            //A match was found
            //Assign the matched foundArray index to the corresponding alphabet
            output += alphabets[i] + ":" + foundArray[i] + ", ";
        }
    }
    return output;
}

```
A really preformant way to do this in linear time is using a HashMap

```
public class Stringray extends MiniJava {

    public static void main(String[] args) {
        String input = "Geben Sie einen Text ein: ";
        System.out.println(countLetters(input));
    }

    public static HashMap<Character, Integer> countLetters(String input) {
        HashMap<Character, Integer> hashMap = new HashMap<>();
        final int length = input.length();
        for (int i = 0; i < length; i++) {
            Character currentChar = Character.toLowerCase(input.charAt(i));
            if (Character.isAlphabetic(currentChar)) {
                if (hashMap.containsKey(currentChar)) {
                    hashMap.put(currentChar, hashMap.get(currentChar) + 1);
                } else {
                    hashMap.put(currentChar, 1);
                }
            }
        }
        return hashMap;
    }

}

```

You need to consider that Upper and Lower case are the same and discard non alphabetic characters. Once you have all the information in the hashmap, the way you show it console is another matter. If you need to show a zero for the letters that are not present in your text it would be better to initialize the hashmap to that.