/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static String looksay(String s){ String a = ""; int count = 0; for(int i=0;i<s.length();i++){ char current = s.charAt(i); if(i==0){ count++; } else { char prev = s.charAt(i-1); if(prev == current){ count++; }else { a+=count; a=a+prev; count=1; prev=current; } } } a+=count; a=a+(char)s.charAt(s.length()-1); return a; } public static void main (String[] args) throws java.lang.Exception { String s="1"; for(int i=0;i<100;i++){ s=looksay(s); System.out.println(s); } } }
Wednesday, March 9, 2016
Look and say sequence algorithm
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!");
}
}
Subscribe to:
Posts (Atom)