LeetCode (54) Valid Anagram
2015-09-23 19:42
183 查看
题目描述
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.
题目要求判断两个string是否具有相同的字母(包括字母出现的次数)。
解题思路
我们可以遍历字符串s,逐一判断s中的字母是否在t中,若在,则删除改字母。若t中查找不到该字母,则返回false;若s遍历完后t不为空,则也返回false,否则返回true。class Solution { public: bool isAnagram(string s, string t) { if(s.size() != t.size()) return false; for(string::iterator it=s.begin(); it != s.end(); ++it) { int pos = t.find_first_of(*it); if(pos == string::npos) return false; else t.erase(pos, 1); } if(t.empty()) return true; else return true; } };
相关文章推荐
- sicily 1021 couples
- Codeforces Round #321 Kefa and Watch(字符串哈希+线段树)
- 面试相关之链表
- linux 命令 system () 函数详解
- 0923《操作系统教程》实验一 ·DOS命令解释程序的编写
- CoreLocation框架定位详解
- HDU 5120 Intersection(几何模板题)
- 欢迎使用CSDN-markdown编辑器
- UVa 1586 - Molar mass
- 注意身体
- SignalTap Ⅱ使用步骤整理
- 使用JBoss 5.1.0.GA部署项目可能遇到的问题汇总
- zoj3829 Known Notation --- 2014 ACM-ICPC Asia Mudanjiang Regional Contest
- Linux 磁阵性能瓶颈定位过程
- javascript模拟getElementByClassName
- Android设计支持库(Android Design Support Library)
- 实现一个工具类,可以把HBase任意表的任意多的列导出到任意指定的HDFS中
- VS2010+WDK配置要点
- LeetCode(53) Climbing Stairs (剑指Offer->跳台阶、变态跳台阶)
- 数独设计(2)