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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,771评论 0 33
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,900评论 0 23
  • 青衣人出神地盯着太湖边的酒馆,一个身形肥胖的伙夫在烤鱼,香气四溢,食客络绎不绝。 沉思片刻,青衣人问身旁的锦衣人:...
    南柯太瘦阅读 686评论 0 2
  • 大家好,我是IT修真院郑州分院第一期的学员胡嘉杰,一枚正直纯洁善良的WEB前端程序员。 今天给大家分享一下,修真院...
    ithinker5阅读 249评论 0 1
  • 风淡云清的天空, 遥望远方, 不知此刻你在想什么, 是否也在望着同一片天。 炎热给人带来了更多庸懒, 不去想任何事...
    lemei阅读 277评论 0 2