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. zookeeper3.4.5集群安装

    机器配置: 机器 Hostname user 192.168.169.139 node139 hadoop 192.168.169.140 node140 hadoop 192.168.169.141 ...

  2. MySQL数据库Date型数据插入问题

    MySQL数据库中,Date型数据插入问题,总是提示如下错误: “java.util.Date cannot be cast to java.sql.Date” 解决办法: 1.首先,获取Date型数 ...

  3. django生成json

    好方便啊……list什么的一下都变成json了呢! import json from django.core.serializers.json import DjangoJSONEncoder def ...

  4. 在w3cschool学完html,css,javascript,jquery以后,还是不会做前端怎么办?

    w3cschool是一个非盈利性的在线技术学习网站,提供按W3C标准编写的基础教程.完整的看完w3cschool上面的手册,可以基本掌握编程语法.基础性的东西通常都会比较零散,因此,在学习一段时间后, ...

  5. 百度feed 寒假实习 一面二面(offer)

    一面(1小时) 自我介绍,研究方向,本科学校,家乡等等.. 1 介绍了jdd风险登录的比赛,问题背景,建模,特征构建,特征选择,模型等. 个人觉得介绍项目一定要高大上一点,把自己创新或者有创意的点子讲 ...

  6. Rock-Paper-Scissors-Lizard-Spock Python实现

    初学python,实现一些很有意思的小游戏是很能提高编程能力的. Rock-Paper-Scissors-Lizard-Spock http://en.wikipedia.org/wiki/Rock- ...

  7. pycharm 常用配置

    lz提示一下,pycharm中的设置是可以导入和导出的,file>export settings可以保存当前pycharm中的设置为jar文件,重装时可以直接import settings> ...

  8. 在线修改GTID模式

    在线修改GTID模式 1. 在每一台机器上执行命令 SET @@GLOBAL.ENFORCE_GTID_CONSISTENCY = WARN; 这是很重要的一步,必须确保服务器上没有违反GTID规范的 ...

  9. 【Flask】Flask Cookie操作

    ### 什么是cookie:在网站中,http请求是无状态的.也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是哪个用户.cookie的出现就是为了解决这个问题,第 ...

  10. 利用Metasploit进行Linux提权

    利用Metasploit进行Linux提权 Metasploit 拥有msfpayload 和msfencode 这两个工具,这两个工具不但可以生成exe 型后门,一可以生成网页脚本类型的webshe ...