Check if a permutation of a string can become a palindrome.
public class HelloWorld{
public static void main(String []args){
String s="maallyaam";
int [] arr= new int[26];
int odd=0;
for(int i=0;i<s.length();i++){
int index = s.charAt(i)-'a';
System.out.println(index);
if(arr[index]%2==0){
odd++;
} else {
odd--;
}
arr[index]++;
}
if(odd>1)
System.out.println("Cannot form palindrome!");
else
System.out.println("Can form palindrome!");
}
}
What is arr[index]? Where did that arr is filled/ From where it got values?
ReplyDeletearr [index] is filled with 0s at initialization
DeleteThis comment has been removed by the author.
ReplyDelete