CC--Q1.2

1.2 Check Permutation: Given two strings, write a method to decide if one is a permutation of the other.

There is one solution that is 0 (N log N) time. Another solution uses some space, but is O(N) time.

public boolean checkPermutation(String str1, String str2) {
    //O(nlogn)
    if (str1.length() != str2.length())
      return false;

    char[] a = str1.toCharArray();
    char[] b = str2.toCharArray();

    Arrays.sort(a);
    Arrays.sort(b);

    return Arrays.equals(a, b);
}

For O(n) method, use a hash table to check.

你可能感兴趣的:(CC--Q1.2)