Efficient way to find Frequency of a character in a String in java : O(n)

前端 未结 5 536
野的像风
野的像风 2021-02-03 09:48

In a recent interview I was asked to write the below program. Find out the character whose frequency is minimum in the given String ? So I tried by iterating through the string

5条回答
  •  情话喂你
    2021-02-03 10:29

    The process of finding frequency of characters in a String is very easy.
    For answer see my code.

    import java.io.*;
    public class frequency_of_char
    {
        public static void main(String args[])throws IOException
        {
            BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
            int ci,i,j,k,l;l=0;
            String str,str1;
            char c,ch;
            System.out.println("Enter your String");
            str=in.readLine();
            i=str.length();
            for(c='A';c<='z';c++)
            {
                k=0;
                for(j=0;j0)
                System.out.println("The character "+c+" has occured for "+k+" times");
            }
        }
    }
    

提交回复
热议问题