| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- // 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;
- }
|