leetcode 242 Valid Anagram
来源:互联网 发布:js input date 默认值 编辑:程序博客网 时间:2024/06/05 21:05
原题:
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.
大意:
给你一个单词,然后再给你另外一个单词。问你这第二个单词是不是第一个单词打乱顺序的结果。
class Solution{ int has[27];public: bool isAnagram(string s, string t) { if(s.size()!=t.size()) return false; memset(has,0,sizeof(has)); for(int i=0;i<s.size();i++) has[s[i]-'a'+1]++; for(int i=0;i<t.size();i++) { has[t[i]-'a'+1]--; if(has[t[i]-'a'+1]<0) return false; } for(int i=0;i<27;i++) if(has[i]!=0) return false; return true; }};
解答:
如果第二个单词是第一个仅仅单词打乱的结果,那所用的字母和字母的数量应该是一样的,记录一个has表记录用过的字母的数量,判断下一个单词即可
0 0
- 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
- leetcode[242] Valid Anagram
- leetcode 242 :Valid Anagram
- LeetCode 242----Valid Anagram
- 【LeetCode】242Valid Anagram
- Leetcode #242 Valid Anagram
- LeetCode 242:Valid Anagram
- LeetCode 242 Valid Anagram
- LeetCode 242:Valid Anagram
- kafka.common.ConsumerRebalanceFailedException
- EventBus的初级使用介绍
- s3c2416x nandflash的操作手册
- 1.casperjs的安装与环境配置
- UITextField详解
- leetcode 242 Valid Anagram
- 328. Odd Even Linked List
- 移动端利用OpenGL展示3D模型文件STL
- Objective-C位运算符
- 【慕课笔记】U1 类和对象 第4节 JAVA中的成员变量和局部变量
- php函数,static,global关键字及三种变量作用域
- Android的view坐标——一张图
- iOS的动态内存检测
- 完美更改项目名称