Wednesday, March 9, 2016

Check if a permutation of a string can become a palindrome.

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!");  
    }  
 }  

3 comments:

  1. What is arr[index]? Where did that arr is filled/ From where it got values?

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete