【LeetCode】242 - Valid Anagram
2015-08-02 13:56
309 查看
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.
Anagram:Only change the arrangement of the word, but the variety and number of chars in the word are identical.
Solution 1: #include<algorithm> sort
Solution 2: count
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
Note:You may assume the string contains only lowercase alphabets.
Anagram:Only change the arrangement of the word, but the variety and number of chars in the word are identical.
Solution 1: #include<algorithm> sort
class Solution { public: bool isAnagram(string s, string t) { //runtime:76ms sort(s.begin(),s.end()); sort(t.begin(),t.end()); return s==t; } };
Solution 2: count
class Solution { public: bool isAnagram(string s, string t) { //runtime:12ms 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; } };
相关文章推荐
- 报错:LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- Java基础-封装
- JSP和Servlet中的页面跳转和重定向辨析
- codeforces 566F F. Clique in the Divisibility Graph(dp)
- java---设计模式之单例模式
- QT模块简介
- HDU1285确定比赛名次 + 拓扑排序
- hdu 5334 Virtual Participation 构造
- Arch Linux 安装小记
- 搜索引擎搭建:heritrix+lucene
- afx_msg消息响应返回值LRESULT char*在多字节字符集下面就可以转换为CString
- python中的for循环
- linux切换文本-图形模式
- hadoop入门:2.Hadoop-1.12伪分布环境搭建
- 【LeetCode 242】Valid Anagram
- HDU 1081 To The Max(二维最大字段和,转化为一维)
- what's new in vc2015
- -fPIC ppc_85xx-gcc -shared -fPIC liberr.c -o liberr.so
- 暑假集训第三周周六赛 搜索 B - Red and Black红黑瓷片
- 写在8月初