|
|
@@ -0,0 +1,44 @@
|
|
|
+// Given two strings s and t, return true if t is an anagram of s, and false otherwise.
|
|
|
+
|
|
|
+// Example 1:
|
|
|
+// Input: s = "anagram", t = "nagaram"
|
|
|
+// Output: true
|
|
|
+
|
|
|
+// Example 2:
|
|
|
+// Input: s = "rat", t = "car"
|
|
|
+// Output: false
|
|
|
+
|
|
|
+#include <vector>
|
|
|
+#include <string>
|
|
|
+#include <iostream>
|
|
|
+
|
|
|
+using namespace std;
|
|
|
+class Solution
|
|
|
+{
|
|
|
+public:
|
|
|
+ bool isAnagram(string s, string t)
|
|
|
+ {
|
|
|
+ vector<int> table(26);
|
|
|
+ if (s.size() != t.size())
|
|
|
+ return false;
|
|
|
+ for (int i = 0; i < s.size(); i++)
|
|
|
+ {
|
|
|
+ table[(int)s[i] - 'a'] += 1;
|
|
|
+ table[(int)t[i] - 'a'] -= 1;
|
|
|
+ }
|
|
|
+ for (auto i : table)
|
|
|
+ {
|
|
|
+ if (i != 0)
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+int main()
|
|
|
+{
|
|
|
+ string s = "car";
|
|
|
+ string t = "rat";
|
|
|
+ Solution so;
|
|
|
+ cout << so.isAnagram(s, t) << endl;
|
|
|
+}
|