LeetCode 242. Valid Anagram

https://leetcode.com/problems/valid-anagram/description/

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?

  • 字符串处理,频率统计题。
  • 第一种方法是对字符串按字母排序,然后比较是否相等,简洁明了。
  • 第二种方法是用hash table。此题指明只有小写字母,所以‘a'-'z'26个英文字母,做张表就可以了。
  • 注意判断方法有两种,一种是都统计完再判断,一种是对s做加法,对t做减法,同时做减法时如果判断有负数统计出现,说明t中出现了多余字母时s中没有的。
  • https://leetcode.com/problems/valid-anagram/solution/
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 
 9 #include <iostream>
10 #include <cstring>
11 #include <vector>
12 using namespace std;
13 
14 class Solution {
15 public:
16     // STL sort
17     bool isAnagram(string s, string t) {
18         sort(s.begin(), s.end());
19         sort(t.begin(), t.end());
20         
21         return s == t;
22     }
23     
24     // Hash
25     bool isAnagram2(string s, string t) {
26         if (s.length() != t.length()) {
27             return false;
28         }
29         
30         vector<int> dict(26);
31         
32         for (auto ch : s) {
33             ++ dict[ch - 'a'];
34         }
35         
36         for (auto ch : t) {
37             -- dict[ch - 'a'];
38         }
39         
40         for (auto iter : dict) {
41             if (iter != 0)
42                 return false;
43         }
44         
45         return true;
46     }
47     
48     bool isAnagram3(string s, string t) {
49         if (s.length() != t.length()) {
50             return false;
51         }
52         
53         vector<int> dict(26);
54         
55         for (auto ch : s) {
56             ++ dict[ch - 'a'];
57         }
58         
59         for (auto ch : t) {
60             -- dict[ch - 'a'];
61             
62             if (dict[ch - 'a'] < 0) // Extra characters in string t
63                 return false;
64         }
65         
66         return true;
67     }
68 };
69 
70 int main(int argc, char* argv[])
71 {
72     Solution    testSolution;
73     int result = 1;
74     
75     result = result && testSolution.isAnagram("anagram", "nagaram");
76     result = result && testSolution.isAnagram("program", "margorp");
77     result = result && !testSolution.isAnagram("rat", "car");
78     result = result && !testSolution.isAnagram("aaab", "aaa");
79     result = result && testSolution.isAnagram("", "");
80     
81     result = result && testSolution.isAnagram2("anagram", "nagaram");
82     result = result && testSolution.isAnagram2("program", "margorp");
83     result = result && !testSolution.isAnagram2("rat", "car");
84     result = result && !testSolution.isAnagram2("aaab", "aaa");
85     result = result && testSolution.isAnagram2("", "");
86 
87     result = result && testSolution.isAnagram3("anagram", "nagaram");
88     result = result && testSolution.isAnagram3("program", "margorp");
89     result = result && !testSolution.isAnagram3("rat", "car");
90     result = result && !testSolution.isAnagram3("aaab", "aaa");
91     result = result && testSolution.isAnagram3("", "");
92 
93     if (result)
94         cout << "ALl tests pass!" << endl;
95     else
96         cout << "Tests fail!" << endl;
97     
98     return 0;
99 }
View Code

 

posted on 2018-01-27 17:39  浩然119  阅读(144)  评论(0编辑  收藏  举报