/* * @author : P programs
* Check if a given string becomes palindrome after rotation or not.
* Exp : mmo , aaplaaeep , travel
*
*We can check for the count of each character in a string , If the total count of 2 chars are equal to
1 then the string can't be palindrome.
*/
* Check if a given string becomes palindrome after rotation or not.
* Exp : mmo , aaplaaeep , travel
*
*We can check for the count of each character in a string , If the total count of 2 chars are equal to
1 then the string can't be palindrome.
*/
package pprograms;
import java.util.HashMap;
import java.util.Scanner;
public class CheckStringRotationPalindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String ");
String str = sc.next();
HashMap<Character , Integer> hs = new HashMap<Character, Integer>();
int len = str.length();
int count = 0;
for(int i = 0 ; i < len ; i++ ){
if(hs.get(str.charAt(i))!=null){
hs.put(str.charAt(i), hs.get(str.charAt(i))+1);
}else{
hs.put(str.charAt(i), 1);
}
}
for(int i = 0 ; i < len ; i++ ){
if(hs.get(str.charAt(i))==1){
count ++;
if(count == 2){
break;
}
}
}
if(count ==1 ){
System.out.println("String can become palindrome after rotation");
}else{
System.out.println("String can't become palindrome even after rotation");
}
}
}
No comments:
Post a Comment