Your current solution is quadratic to the size of the input string or O (n ^ 2)
You can solve this more efficiently by counting the appearance of each character in the string and then counting the number of substrings that can be created with that character.
eg. if the character occurs 4 times, then this leads to 3 + 2 + 1 = 6 substrings.
To do this, you can use the following formula: ((n-1) * n) / 2
Thus, the complexity of the algorithm comes down to O (n), because to count each character you only need to cross the line once.
I believe this code should work:
public static void main(String[] args) { String str = "xyzxyzxyzxyz"; Map<Character, Integer> map = new HashMap<>(); for (char c : str.toCharArray()) { Integer count = map.get(c); if (count == null) count = 0; map.put(c, count + 1); } int sum = 0; for (int n : map.values()) sum += ((n - 1) * n) / 2; System.out.println(sum); }
source share