Hey Please refer to following question: Question Link
I used the HashMap to store frequency of each character and then i stored the frequency in array and we can only remove one letter from string and check if string contains equal number of characters or not. My code is passing all test case except the following provided in link:
static String isValid(String s) {
Map<Character,Integer> freq=new HashMap<Character,Integer>();
List<Integer> list=new ArrayList<>();
char[] chars=s.toCharArray();
for(char c:chars){
Integer i=freq.get(c);
if(i==null){
freq.put(c,1);
}
else{
freq.put(c,i+1);
}
}
for(Integer c:freq.values()){
list.add(c);
}
int[] arr=new int[list.size()];
for(int i=0;i<list.size();i++){
arr[i]=list.get(i);
}
int count=0;
int n=arr.length;
for(int i=0;i<list.size()-1;i++){ //2 2 1 1
if(arr[i]==arr[i+1]){}
else{
if(arr[i]-arr[i+1]==1 || arr[i+1]-arr[i]==1 || arr[i]-arr[i+1]==arr[i]-1 || arr[i+1]-arr[i]==arr[i+1]-1){
arr[i+1]-=1;
count++;
}
else{
return "NO";
}
}
}
if(count==1 || count<1){
return "YES";
}
else
return "NO";
}
The frequency count of the file is
{p=1, a=11111, b=11111, c=11111, d=11111, e=11111, f=11111, g=11111, h=11111, i=11111}
So you need to handle the case when you only have a single character and the rest are equal. By deleting the single character p
, you get a valid solution.