The problem statement is given here
I took an array of 26 integers , each block or array represent a character. (arr[0] for a , arr[1] for b ... arr[25] for z ).
Now we scan the string character by character and increment the counter of the array according to the character appears. and at last print the characters and their count if the count is greater than 1.
import java.util.*;
class Main{
public static void main(String args[]){
Scanner s =new Scanner(System.in);
String st=s.next();
int arr[]=new int[26];
for(int i=0;i<=25;i++){
arr[i]=0;
}
for(int i=0;i<st.length();i++){
int j=(int)st.charAt(i);
arr[j-97]+=1;
}
for(int i=0;i<26;i++){
if(arr[i]>1){
System.out.println(((char) (i+97))+" count : "+arr[i]);
}
}
}
}
I took an array of 26 integers , each block or array represent a character. (arr[0] for a , arr[1] for b ... arr[25] for z ).
Now we scan the string character by character and increment the counter of the array according to the character appears. and at last print the characters and their count if the count is greater than 1.
import java.util.*;
class Main{
public static void main(String args[]){
Scanner s =new Scanner(System.in);
String st=s.next();
int arr[]=new int[26];
for(int i=0;i<=25;i++){
arr[i]=0;
}
for(int i=0;i<st.length();i++){
int j=(int)st.charAt(i);
arr[j-97]+=1;
}
for(int i=0;i<26;i++){
if(arr[i]>1){
System.out.println(((char) (i+97))+" count : "+arr[i]);
}
}
}
}
This code doesn't handle if uppercase letters are given.
ReplyDeletetake array of 256 size ,it will work.
Delete