leetcode--242-- Valid Anagram
2016-03-22 17:56
197 查看
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.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?class Solution {
public:
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;
}
};
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?class Solution {
public:
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;
}
};
相关文章推荐
- 网易有道面试题集锦
- 网易有道笔试题2
- 1到n中1的个数
- Uva 101:The Blocks Problem(木块问题)
- 用java代码实现,使用5升和6升的水杯装出3升的水
- UVa-511-Way to SanJose?
- UVa-1598-Exchange
- UVa 210 Concurrency Simulator
- 求数组中出现次数大于N/2的元素(majority number)
- LeetCode_N-Queens
- LeetCode_Jump Game II
- LeetCode_Best Time to Buy and Sell Stock
- LeetCode_Best Time to Buy and Sell Stock II
- 十大编程算法助程序员走上高手之路
- 数据结构和算法经典100题-第13题
- 数据结构和算法经典100题-第25题
- 2015年百度一面试题
- 面试时算法题的解答思路
- 经典算法题一览
- C#实现N*N的顺时针旋转矩阵