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. Config Static IP Address manually in Ubuntu

    The process of the configuration of static IP address in Ubuntu is as follows: ``` $ sudo vim /etc/n ...

  2. s5_day8作业

    # 1 整理今天装饰器代码(每人手写一份,注意,是手写,交到小组长手里,明天我检查),准备明天默写 # 2 编写日志装饰器,实现功能如:一旦函数f1执行,则将消息2017-07-21 11:12:11 ...

  3. Powerdesiger使用技巧

    1.问题:使用CDM在生成LDM文件时,每次都生成Name(LDM)1,Name(LDM)2这样 , 同时遇到一个莫名其妙的问题,就是LDM或者PDM生成到了整个工程外的目录下 疑问回答:是因为原有的 ...

  4. Mysql数据表字段设置了默认值,插入数据后默认字段的值却为null,不是默认值

    我将mysql的数据表的某个字段设置了默认值为1,当向该表插入数据的时候该字段的值不是默认值,而是null. 我的错误原因: 对数据库的操作我使用了持久化工具mybatis,插入数据的时候插入的是整个 ...

  5. html 基础--一般标签

    <html>    --开始标签 <head> 网页上的控制信息 <title>页面标题</title> </head> <body& ...

  6. JAVA发送HttpClient

    http://bijian1013.iteye.com/blog/2310211 在发送HTTP请求的时候会使用到POST和GET两种方式,如果是传送普通的表单数据,我们直接将参数到一个Key-val ...

  7. 使用GoogleCode作SVN服务器的一些问题及解决办法

    1.首先最主要的一个问题,就是注册GoogleCode和安装SVN工具. 网上教程很多,不一一赘述.http://www.th7.cn/Program/net/201305/136059.shtml ...

  8. 无线安全之破解WPA/WPA2 加密WiFi

    准备 可以使用无线网络的Kali Linux 由于古老的WPE加密的WiFi已经几乎没有了,所以这里我就不去细说如何破解WPE加密的WiFi了.今天就来聊聊 如何来使用Kali Linux来破解Wpa ...

  9. Spring 之高级装配

    [环境与Profile] 暂略 [条件化的bean] 暂略 [处理自动装配歧义性] 暂略 [ bean 的作用域] 在 @Componen . @Bean 下以及 XML 中的声明方式如下所示, @C ...

  10. 【Java】仿真qq尝试:用户注册(二)

    参考: 1.corejavaI:使用解耦的try/catch与try/finally 2.Java中try catch finally语句中含有return语句的执行情况(总结版):http://bl ...