242. Valid Anagram
来源:互联网 发布:dota2公开比赛数据 编辑:程序博客网 时间:2024/05/18 16:14
Given two strings s and t, write a function to determine if t is an anagram of s.
看一个数是不是另一个数的anagram
anagram :组成元素相同,但顺序不同
For example,
s = “anagram”, t = “nagaram”, return true.
s = “rat”, t = “car”, return false.
Note:
You may assume the string contains only lowercase alphabets.
假设都是小写字母
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
如果你的字符串中有 unicode 字符,可以解决吗?
思路1
排序然后比较
class Solution {public: bool isAnagram(string s, string t) { sort(s.begin(), s.end()); sort(t.begin(), t.end()); return s==t; }};
思路2
利用数组计数可以判断元素想等问题
class Solution {public: bool isAnagram(string s, string t) { vector<int> v(26,0); for(int i=0; i<s.size(); i++) { v[s[i]-'a']++; } for(int i=0; i<t.size(); i++) { v[t[i]-'a']--; } for(int i=0; i<26; i++) if(v[i]!=0) return 0; return 1; }};
0 0
- [leetcode] 242.Valid Anagram
- 242.Valid Anagram
- leetcode 242. Valid Anagram
- 242. Valid Anagram
- [leetcode] 242. Valid Anagram
- leetCode 242. Valid Anagram
- Leetcode 242. Valid Anagram
- 242. Valid Anagram
- 242. Valid Anagram LeetCode
- 242. Valid Anagram
- LeetCode 242. Valid Anagram
- LeetCode 242. Valid Anagram
- 242. Valid Anagram
- 242. Valid Anagram
- 242. Valid Anagram
- 242. Valid Anagram
- 242. Valid Anagram
- 242. Valid Anagram
- 如何在调试Window App时,触发 Suspending ,Resuming 等事件
- POJ 1741Tree
- CentOS6.5环境使用keepalived实现nginx服务的高可用性及配置详解
- 在二维数组中查找一个数
- Leetcode 147. Insertion Sort List
- 242. Valid Anagram
- More Effective C++----(6)自增、自减操作符前缀形式和后缀形式的区别
- 1年日记(5月篇)
- 学习Unity射线的创建使用
- 样式和主题
- java并发编程(14)--java的守护线程与非守护线程
- HDU 1544Palindromes(求回文字符串)
- Partition List
- RS WLAN产品测试全面解决方案