242. Valid Anagram
来源:互联网 发布:淘宝开网店用18 编辑:程序博客网 时间:2024/06/05 16:12
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?
s思路:
1. 区别一下anagram是字母一样,但顺序不同;parlindrome是回文。容易搞混。
2. 由于只有lowercase.因此,用26长的vector来存出现的次数,等效于hash。
3. 经常需要用数组代替hash,只要元素个数已知,且是数字!
//方法1:用数组代替hashtableclass Solution {public: bool isAnagram(string s, string t) { // if(s.size()!=t.size()) return false; vector<int> v(26,0); for(char c:s){ v[c-'a']++; } for(char c:t){ if(v[c-'a']--==0) return false; } return true; }};
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
- [南阳OJ-No.24]素数距离问题|现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度。如果左右有等距离长度素数,则输出左侧的值及相应距离。 如果输入的整数本身就是素
- jsp页面传值
- php中操作数据库的基本代码和流程
- 利用Samba软件windows和linux共享文件
- 【前端】-HTML
- 242. Valid Anagram
- hadoop2.x单机搭建分布式集群超详细教程
- 瀑布流无法用<c:forEach>循环遍历
- 分布式多爬虫系统——架构设计
- Spring mvc:annotation-driven
- OSGI企业应用开发(十四)整合Spring、Mybatis、Spring MVC
- C语言------栈的初体验(数组实现)
- 为ubuntu添加有道词典翻译
- cxf Webservice