Checking if two strings are permutations of each other

前端 未结 30 1071
感情败类
感情败类 2020-12-05 08:19

How to determine if two strings are permutations of each other

30条回答
  •  感动是毒
    2020-12-05 08:53

    Best Way to do this is by sorting the two strings first and then compare them. It's not the most efficient way but it's clean and is bound to the runtime of the sorting routine been used.

    boolean arePermutation(String s1, String s2) { 
      if(s1.lenght() != s2.lenght()) {
         return false;
       }
       return mySort(s1).equals(mySort(s2));
     }
    
      String mySort(String s) {
       Char letters[] = s.toCharArray();
       Arrays.sort(letters);
       return new String(letters);
    }
    

提交回复
热议问题