Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,151 questions

40,705 answers

573 users

How to to check if all the characters of a string are with same frequencies in Java

1 Answer

0 votes
public class MyClass {
    static boolean same_frequencies(String s) { 
        int[] letters = new int[256];
        int len = s.length();
           
        for (int i = 0; i < len; i++) {
             if (Character.isLetter(s.charAt(i)))
                 letters[(int)s.charAt(i)]++;
        }
      
        int frequencies = 0; 
        for (int i = 0; i < 256; i++) { 
            if (letters[i] != 0) {
                frequencies = letters[i];
                break;
            }
        }
     
        for (int i = 0; i < 256; i++) { 
            if (letters[i] != 0 && (letters[i] != frequencies)) {
                return false;
            }
        } 
        return true;
    } 
    public static void main(String args[]) {
        String s = "aaabbbcccwww"; 
          
        if (same_frequencies(s))
            System.out.println("yes");
        else
            System.out.println("no");
    }
}



/*
run:

yes

*/

 





answered Jan 1, 2020 by avibootz
...