Find duplicate characters in a String and count the number of occurances using Java

前端 未结 30 2526
遇见更好的自我
遇见更好的自我 2020-12-14 11:47

How can I find the number of occurrences of a character in a string?

For example: The quick brown fox jumped over the lazy dog.

Some example

30条回答
  •  佛祖请我去吃肉
    2020-12-14 12:37

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            String reverse1;
            String reverse2;
            int count = 0;
            while(n > 0)
            {
                String A = sc.next();
                String B = sc.next();
                reverse1 = new StringBuffer(A).reverse().toString();
                reverse2 = new StringBuffer(B).reverse().toString();
                if(!A.equals(reverse1))
                {
                    for(int i = 0; i < A.length(); i++)
                    {
                        for(int j = 0; j < A.length(); j++)
                        {
                            if(A.charAt(j) == A.charAt(i))
                            {
                                count++;
                            }
                        }
                        if(count % 2 != 0)
                        {
                            A.replace(A.charAt(i),"");
                            count = 0;
                        }
                    }
                    System.out.println(A);
                }
                n--;
            } 
        }
    }
    

提交回复
热议问题