Learn-dsa..in 30 days!



























CC-20 : Check if input String can have characters of equal frequency by removing 0 or 1 characters.

Given an input String str, check if str can have characters of equal frequency by removing 0 or 1 characters.

Test cases and expected outputs:

Input Parameters Expected outputs
str="aazzecc"; The input string can have characters of equal frequency by removing 0 or 1 characters.
str1="aaz"; The input string can have characters of equal frequency by removing 0 or 1 characters.
str1="az"; The input string can have characters of equal frequency by removing 0 or 1 characters.
str1="az"; The input string *cannot* have characters of equal frequency by removing 0 or 1 characters

Pseudocode:

The java method should accept following input parameters: str (String).
Initialize a variable chrFreqMap of type HashMap to hold characters from input String as keys, and their frequencies as values.
Iterate through arr using a for loop with idx as a loop variable with initial value 0 and increment idx till it reaches arr.length-1:
If chrFreqMap does not contain key str.charAt(idx), then put key-value pair str.charAt(idx) and 1, to chrFreqMap.
If chrFreqMap contains key str.charAt(idx)with value freq, then put key-value pair str.charAt(idx) and freq+1 to chrFreqMap.
Initialize a variable freqChrMap of type HashMap to hold frequencies from chrFreqMap as keys, and their count as values.
Iterate through values of chrFreqMap and for each value:
If freqChrMap contains key str.charAt(idx)with value freq, then put key-value pair str.charAt(idx) and freq+1 to freqChrMap.
If size of freqChrMap < 1 or > 2, then str cannot have characters of equal frequency by removing 0 or 1 characters. Return false.
If size of freqChrMap ==1, then str can have characters of equal frequency by removing 0 or 1 characters Return true.
If size of freqChrMap ==2:
str can have characters of equal frequency by removing 0 or 1 characters :
If any of the 2 frequencies have frequency 1 and count 1. Return true.
If difference of frequencies is 1. Return true.
After above loop completes, and we have returned true from within the loops, then str can have characters of equal frequency by removing 0 or 1 characters. If we have not returned true from within the loops, then str cannot have characters of equal frequency by removing 0 or 1 characters. Return false.

Code:

public boolean hashMapCheckFreq(String str) throws Exception{
	HashMap< Character,Integer> chrFreqMap=new HashMap< Character, Integer>();
	for (int idx=0; idx < str.length(); idx++) {
		chrFreqMap.put(str.charAt(idx), chrFreqMap.getOrDefault(str.charAt(idx),0)+1);
	}		
	HashMap< Integer,Integer> freqChrCntMap=new HashMap< Integer, Integer>();
	Iterator< Integer> itr=chrFreqMap.values().iterator();
	Integer freq=null;
	while (itr.hasNext()) {
		freq=itr.next();
		freqChrCntMap.put(freq, freqChrCntMap.getOrDefault(freq, 0)+1);
	}
	if (freqChrCntMap.size() < 1 || freqChrCntMap.size() > 2  ) {
		return false;
	}
	if (freqChrCntMap.size() == 1 ) {
		return true;
	}
	int freq1, freq2, cnt1, cnt2;
	if (freqChrCntMap.size() == 2 ) {
		itr=freqChrCntMap.keySet().iterator();
		freq1=itr.next().intValue();
		cnt1=freqChrCntMap.get(freq1);
		freq2=itr.next().intValue();
		cnt2=freqChrCntMap.get(freq2);
		if ((freq1==1 && cnt1==1)||(freq2==1 && cnt2==1)) {
			return true;
		}
		if (Math.abs(freq1-freq2)==1) {
			return true;
		}
	} 
	return false;		
}

Click here to download and run code and test cases !