LeetCode-242
Links:https://leetcode.com/problems/valid-anagram/
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.
题意:就是判断字符串s和t是变位词。
思路1:排序判断相等。(n*logn + n)
思路2:计数判断相等。(2*n + 26)(未做…)
1 2 3 4 5 6 7 8 |
class Solution { public: bool isAnagram(string s, string t) { sort(s.begin(),s.end()); sort(t.begin(),t.end()); return s==t; } }; |
【LeetCode】242. Valid Anagram