Intuition:
→ hash table: good way to count the frequency → each character is key and frequency is the value
Algorithm(Greedy hashtable):
class Solution{
public int longestPanlidrome(String s){
// Map to store freqnency of occurence of each charater
Map<Character, Integer> frequencyMap = new HashMap<>();
// Count frequencies
for(char c : s.toCharArray()){
frequencyMap.put(c, frequencyMap.getOrDefault(c,0) + 1)
}
int res = 0;
boolean hasOddFrequency = false;
// freq = values of hashmap
for (int freq : frequencyMap.values()){
//check if frequency is even
if((freq % 2) == 0){
res += freq;
} else{
res += freq - 1;
hasOddFrequency = true;
}
}
// If hasOddFrequency is true, we have at least one unmatched
// character to make the center of a odd length palidrome
if(hasOddFrequency) return res + 1;
return res;
}
}
With two given integer arrays num1 and num2 sorted in non-decreasing order
m,n represent the number of elements in nums1 and nums2
final sorted array should not be returned by the function, but instead be stored in the arrary nums1, length n+m
repetition should be counted