I take Apply online assessement through Codesignal. I did not pass 5 hidden test cases.
Question:
Given a string word as input, return the minimum number of characters added into the word to become anagram - become a palindrome.
For example, if word = "apple", it's possible to add two letters 'a' and 'e', which makes the resulting string an anadrome: "appleae" letters can be rearranged to become "apelepa", which is a palindrome.
Here is my solution below, could you guys help point out which part is incorrect?
public int minimum(String s){
int[] letters = new int[26];
for(char c : letters.toCharArray()){
letters[c-'a']++;
}
int res = 0;
int skip = 1; // 1 or 3 repeat characters do not need to add extra one character
for(int cnt : letters){
if(cnt > 3 && cnt%2==1){
res++;
}else if(cnt <= 3 && cnt%2 ==1){
if(skip==1){
skip--;
continue;
}
res++;
}
}
return res;
}