Wednesday, May 25, 2016

How will you check,given two strings are permutation of the other.

Example:    "god"   permutation will be "dog" 

Solution:

 public String sort(String s)
 {
   char[] content = s.toCharArray();
   java.util.Arrays.sort(content);
   return new String(content);
 }

public boolean permutation(String s ,String t)
{
  if(s.lenght() ! = t.length())
 {
   return false;
 }
 return  sort(s).equals(sort(t));
}

Though this solution is not as optimal in some cases.

No comments: