LeetCode 242. Valid Anagram (map使用)
来源:互联网 发布:php页面输出乱码 编辑:程序博客网 时间:2024/06/12 00:34
一、题目
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和t,看s和t是否是anagram。什么是anagram?就是s和t中元素需要一模一样,顺序可以不同
注意:由于两个字符串中的元素需要--对应,当两个字符串的长度不一致直接返回false
同时,字符串的的题字符集的范围,空字符串等情况。题目说只考虑小写字母的情况
思路:将s中元素放入map表中,遍历t的元素是否在表中,查找成功一次相应元素的值--。只要有一个元素不在就返回false
class Solution {public: bool isAnagram(string s, string t) { if(s.size()!=t.size()) return false; //unordered_map<char,int> maps; //hash表实现的效率反而更低? map<char,int> maps; for(auto temps:s) { maps[temps]++; } for(auto tempt:t) { if(maps[tempt]) { maps[tempt]--; } else { return false; } } return true; }};
0 0
- LeetCode 242. Valid Anagram (map使用)
- leetcode 242. Valid Anagram | Map
- [leetcode] 242.Valid Anagram
- leetcode 242. Valid Anagram
- [leetcode] 242. Valid Anagram
- leetCode 242. Valid Anagram
- Leetcode 242. Valid Anagram
- 242. Valid Anagram LeetCode
- LeetCode 242. Valid Anagram
- LeetCode 242. Valid Anagram
- [LeetCode]242. Valid Anagram
- 【LeetCode】242. Valid Anagram
- leetcode 242. Valid Anagram
- leetcode 242. Valid Anagram
- LeetCode--242. Valid Anagram
- LeetCode *** 242. Valid Anagram
- #leetcode#242. Valid Anagram
- leetcode 242. Valid Anagram
- iOS面试笔记(1)
- 【CSS】float属性详解
- Javascript基础知识(一)
- 尚硅谷_JavaScript_学习笔记
- elasticsearch笔记_字段(名词)类型_分析过程(三)
- LeetCode 242. Valid Anagram (map使用)
- Properties
- 推荐系统-推荐冷启动问题
- 关于八进制改十进制程序的修改
- java中的equals( )和hashcode( )
- 手游客户端开发招聘要求
- 常用排序算法之冒泡排序
- applicationContext的xml配置文件异常:找不到beans的声明文件
- redis brpop