LeetCode Algorithms #242 <Valid Anagram>
2016-01-31 17:10
281 查看
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.
思路:
没有一点难度的问题,貌似有个类似的题我用数组实现过了,所以这次用map实现。
解:
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
Note:
You may assume the string contains only lowercase alphabets.
思路:
没有一点难度的问题,貌似有个类似的题我用数组实现过了,所以这次用map实现。
解:
bool isAnagram(std::string s, std::string t) { std::map<char, int> charNum_s; std::map<char, int> charNum_t; if(s.size() != t.size()) { return false; } for(size_t strLength_s = 0; strLength_s < s.size(); strLength_s++) { charNum_s[s[strLength_s]]++; } for(size_t strLength_t = 0; strLength_t < s.size(); strLength_t++) { charNum_t[t[strLength_t]]++; } if(charNum_s.size() != charNum_t.size()) { return false; } for(size_t strLength_s = 0; strLength_s < s.size(); strLength_s++) { if(charNum_s[s[strLength_s]] != charNum_t[s[strLength_s]]) { return false; } } return true; }
相关文章推荐
- POJ 3259/USACO 2006 December Gold:Wormholes
- LeetCode Algorithms #292 <Nim Game>
- 《Learn You Some Erlang for Great Good!》的学习笔记(四)
- projecteuler---->problem=9----Special Pythagorean triplet
- 【HDOJ】4775 Infinite Go
- Tango学习笔记(一)
- Google Chrome v48.0.2564.
- Django-blog-zinnia初体验(一)
- mongodub 组建 mongo mongod mongs 等组件
- django实现文件下载
- ZOJ2855 - Google Map
- gor引流快速入门
- go 为方便单元测试的函数
- go 有限状态机
- Django开发博客(八)——部署在SAE
- Analysis of algorithms: observation
- Golang 学习,第一篇:环境配置
- DJANGO之分页
- django学习笔记(三)模型
- 转发django 1.6 markdown的使用