# Java program to find frequency of characters in a string

In this article, you will understand the how to find the frequency of characters in strings by using Java programming language. Along with this, you will understand the hashing concept in java (Hash map).

### Problem description

You have given a string, and you must find out the frequency of each character in the string and print them.

You need to write a java program for the above problem.

Solution

Approach-1: This problem can be done using hashing technique. You can use the hash map. You need to pass character as key record and integer as value record while creating a hash map. Now the logic is you must store the characters of the given string in the key record. And need to store the count of each character of the string in their respective key-value record. If you encounter the character again, you need to increment the frequency count of that character in the hash map. Finally, you need to traverse the hash map and print the character along with its count values.

Program

``````import java.io.*;
import java.util.*;
import java.lang.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
String repString=sc.next();
FreqString StringFreq=new FreqString();
StringFreq.Frequency(repString);
}
}
class FreqString
{
void Frequency(String s)
{
HashMap<Character,Integer> Freq=new HashMap<>();
for(int i=0;i<s.length();i++)
{
if(Freq.containsKey(s.charAt(i)))
{
Freq.put(s.charAt(i),Freq.get(s.charAt(i))+1);
}
else
{
Freq.put(s.charAt(i),1);

}
}

for(int i=0;i<s.length();i++)
{
if(Freq.get(s.charAt(i))!=0)
{
System.out.println(s.charAt(i)+"->"+Freq.get(s.charAt(i)));
Freq.put(s.charAt(i),0);
}
}
}
}
``````

Output Time complexity: O(n), n is the length of string input.

Space: O(n).

Approach-2: This is a brute force approach where you will take an array to store the frequency of each character in the string. You will need to compare every character of the string with each other.

Generally, this is the brute force approach that one can follow to solve the problem.

The time complexity of this approach is O(n^2) in the worst case. And space complexity is O(1).

Program

``````import java.io.*;
import java.util.*;
import java.lang.*;
public class Main
{
public static void main ( String [] args) {
Scanner sc=new Scanner ( System.in);
String repString = sc.next ();
FreqString StringFreq = new FreqString ();
StringFreq.Frequency( repString );
}
}
class FreqString
{
void Frequency( String s )
{
int [] freq = new int[s.length()];
char[] str = s.toCharArray();
for(int i= 0 ; i < s.length () ; i++)
{
int c = 1;
for (int j = i+1; j < s.length(); j++)
{
if ( str[i] == str[j] )
{
c++;
str[j] = '0';
}
}
freq[i] = c;
}
for(int i =0;  i < freq.length ;i++)
{
if(str[i] !=' ' &&  str[i] !='0')
{
System.out.println ( str[i] +" -> " + freq[i]);
}
}
}
}
``````

Output If you want to perform any operation on a string, then you need to understand the concept of mutable and immutable.

Strings are immutable. You cannot directly change the values in the string, that is, adding or removing any values from the string. To perform this kind of operation then, you need to convert the string into another mutable data type.

So, you need to use type conversion to change the string data type into another data type. You need to change the string data type to char array to perform the addition or removal of characters from the string.

In the above program, if you try to compare each character of the string with others, then you will get an error, but if you convert the string to an array type, then you can perform these operations.

In a programming language, the term mutable refers to the feasibility of a data type or data structure to change its values during the run time. Or simply if we can change the values of a component during the run time execution, then we say that component is mutable.

Ex: list, sets, arrays etc.

While coming to the concept of immutable, if we are unable to change the values of a component during the run time of a program, then we classify those components into immutable contents.

Ex: Strings.