题目

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.

Note:

You may assume the string contains only lowercase alphabets.

分析

判断给定两个字符串是否为相同字母不同排列的单词。

最简单的办法就是调用stl的排序函数sort给两个字符串s和t排序,然后比较是否相等即可,复杂度为O(nlogn);

如果嫌弃复杂度太高,还可以采用另外一种办法求每个字符个数判相等(题目已经假设只有小写字母那么也就只有26个),比较是否相等,复杂度为O(n);

AC代码

class Solution {
public:
//方法一:排序判断
bool isAnagram1(string s, string t) {
if (s.empty() && t.empty())
return true;
else if (s.empty() || t.empty())
return false; sort(s.begin(), s.end());
sort(t.begin(), t.end()); if (s == t)
return true;
return false;
}
//方法二:字母个数判相等
bool isAnagram(string s, string t) {
vector<int> count(26, 0);
for (int i = 0; i < s.size(); i++)
count[s[i] - 'a'] ++;
for (int i = 0; i < t.size(); i++)
count[t[i] - 'a'] --;
for (int i = 0; i < 26; i++)
if (count[i] != 0)
return false;
return true;
}
};

GitHub测试程序源码

LeetCode(242)Valid Anagram的更多相关文章

  1. LeetCode之旅(13)-Valid Anagram

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

  2. LeetCode(36)Valid Sudoku

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  3. LeetCode(49)-Valid Parentheses

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  4. LeetCode(125) Valid Palindrome

    题目 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ign ...

  5. LeetCode(65) Valid Number

    题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...

  6. LeetCode(20)Valid Parentheses

    题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  7. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  8. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  9. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

随机推荐

  1. 爬虫(GET)——handler处理器和自定义opener

    工具:python3 解释:urlopen()不支持代理.cookie等其他的http/https高级功能,所以需要handler处理器创建特定功能的处理器对象,urllib.request.buli ...

  2. Windows和Ubuntu使用网线直连搭建局域网

    1.Windows下的配置:右键右下角的网络图标(或者右键网络→属性)→更改适配器设置→以太网→右键属性→TCP/IPv4→IP地址(192.168.1.3)→子网掩码(255.255.255.0)→ ...

  3. (转)生产环境常见的HTTP状态码列表(老男孩整理)

    生产环境常见的HTTP状态码列表(老男孩整理) 原文:http://blog.51cto.com/oldboy/716294 ##################################### ...

  4. Java面向对象(static、final、匿名对象、内部类、包、修饰符、代码块)

    面向对象 今日内容介绍 u final u static u 匿名对象 u 内部类 u 包的声明与访问 u 四种访问修饰符 u 代码块 第1章 final关键字 1.1 final的概念 继承的出现提 ...

  5. 使用AuthToken架构保护用户帐号验证Cookie的安全性

    在项目或者网站开发中,我们很多人很多时候喜欢使用微软的FormsAuthentication类的GetAuthCookie函数生成需要在访客客户端放置的帐号校验Cookie,这个本身没问题,但是很多人 ...

  6. Thread 和 Runnable创建新线程的区别,Runnable可以共享线程类的实例属性

    Thread实现多线程: public class Thread2 extends Thread{ public  int i; public void run(){ for(; i < 100 ...

  7. C#小记

    1.背景:用fileinput 上传文件 直接上传文件,但有时会发现,这个不上传文件也是可以携带其他参数的, 如果直接用: uploadFile = context.Request.Files[]; ...

  8. Got error 28 from storage engine的错误处理

    早上例行检查数据库,发现Got error 28 from storage engine这个错误,天那,我的数据.心里哇凉....备份的时间还是很久以前.最近更新了不少,麻烦大了. 好在找到了解决方法 ...

  9. 洛谷 P2038 无线网络发射器选址

    题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129 条东西向街道和129 条南北向街道所形成的网格状,并且相邻 ...

  10. WPF中,DataGrid最左边多出一行的解决方案

        这种情况下,请在DataGrid的属性里加上这个属性:   RowHeaderWidth="0" 必须赋值为0,不能不赋值,也不能赋其他值. 问题解决.