Given two strings s and t, write a function to determine if t is an anagram of s.

For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.

题目的意思就是是否两个字符串之间可以通过颠倒次序来实现:

 class Solution {
public:
bool isAnagram(string s, string t) {
int szS = s.size();
int szT = t.size();
if (szS != szT)return false;
sort(s.begin(), s.end());
sort(t.begin(), t.end());
for(int i = ; i < szS; ++i){
if (s[i] != t[i])
return false;
}
return true;
}
};

再看一遍题目感觉上面写的有点蠢了, 其实排序完成之后直接比较两个字符串是否相等就可以了,不过这种排序之后再比较的时间复杂度为O(NlogN),可以通过计数的方尝试将复杂度降低到O(N),两种方式的java代码如下所示:

第一种:排序

 public class Solution {
public boolean isAnagram(String s, String t) {
char[] sArr = s.toCharArray();
char[] tArr = t.toCharArray();
Arrays.sort(sArr);
Arrays.sort(tArr);
return String.valueOf(sArr).equals(String.valueOf(tArr));
}
}

第二种: 计数法

 public class Solution {
public boolean isAnagram(String s, String t) {
char [] count = new char [26];
for(int i = 0; i < s.length(); ++i){
count[s.charAt(i)-'a']++;
}
for(int i = 0; i < t.length(); ++i){
count[t.charAt(i)-'a']--;
}
for(int i = 0; i < 26; ++i){
if(count[i] != 0)
return false;
}
return true;
}
}

计数法的runtime实际上比前面的排序方法号上很多,但是有一点吐槽一下,现在leetCode上面的runtime为什么都是java比c++要快上很多啊,java现在有这么快吗,有点不能理解啊。

LeetCode OJ:Valid Anagram(有效字谜问题)的更多相关文章

  1. 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)

    22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...

  2. [leetcode]242. Valid Anagram验证变位词

    Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...

  3. LN : leetcode 242 Valid Anagram

    lc 242 Valid Anagram 242 Valid Anagram Given two strings s and t, write a function to determine if t ...

  4. LeetCode 242. Valid Anagram (验证变位词)

    Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...

  5. [LeetCode] 242. Valid Anagram 验证变位词

    Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...

  6. LeetCode 242 Valid Anagram

    Problem: Given two strings s and t, write a function to determine if t is an anagram of s. For examp ...

  7. (easy)LeetCode 242.Valid Anagram

    Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...

  8. Java [Leetcode 242]Valid Anagram

    题目描述: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...

  9. Leetcode 242. Valid Anagram(有效的变位词)

    Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...

  10. Leetcode 242 Valid Anagram pytyhon

    题目: Given two strings s and t, write a function to determine if t is an anagram of s. For example,s  ...

随机推荐

  1. Deep Learning -- 数据增强

    数据增强 在图像的深度学习中,为了丰富图像训练集,更好的提取图像特征,泛化模型(防止模型过拟合),一般都会对数据图像进行数据增强,数据增强,常用的方式,就是旋转图像,剪切图像,改变图像色差,扭曲图像特 ...

  2. Android Wear - Design Principles for Android Wear(设计原则)

    ---------------------------------------------------------------------------------------------------- ...

  3. windows如何安装mysql

    参考一下网址,已测试可用 https://www.cnblogs.com/reyinever/p/8551977.html

  4. mysql安装前的系统准备工作

    一.系统环境总结:

  5. redis---在CentOS6.5下安装与配置

    本文详细介绍redis单机单实例安装与配置,服务及开机自启动.如有不对的地方,欢迎大家拍砖o(∩_∩)o (以下配置基于CentOS release 6.5 Final, redis版本3.0.2 [ ...

  6. 集成ssm+shiro出现的 问题

    1.springmvc-servlet.xml .applicationContext.xml该如何配置include和exclude?,目前的做法是将.applicationContext.xml全 ...

  7. No module named bz2

    yum install bzip* python2.6 import bz2 python2.7 import bz2 error 解决:sudo cp /usr/lib64/python2.6/li ...

  8. StringUtils用法(isNotEmpty和isNotBlank)

    isNotEmpty将空格也作为参数,isNotBlank则排除空格参数 参考 Quote StringUtils方法的操作对象是java.lang.String类型的对象,是JDK提供的String ...

  9. React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发

    React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发   2016/09/23 |  React Native技术文章 |  Sky丶清|  4 条评论 |  1 ...

  10. 配置内核 Makefile:1449: *** mixed implicit and normal rules. Stop.【转】

    本文转载自:https://blog.csdn.net/bitowang/article/details/8446494 在编译内核的时候提示Makefile:1449: *** mixed impl ...