Description
Given two strings
s
and
For example,
s = “anagram”, t = “nagaram”, return true.
s = “rat”, t = “car”, return false.
Note:
You may assume the string contains only lowercase alphabets.
Analysis
let a table count the times every letter appears. Specifically, for string
s
we increase the numbers while for string
Code
class Solution {
public:
bool isAnagram(string s, string t) {
int count[26]={0};
for (char c : s) count[c-97]++;
for (char c : t) count[c-97]--;
for (int i : count)
if (i != 0) return false;
return true;
}
};
Appendix
- Link: https://leetcode.com/problems/excel-sheet-column-number/
- Run Time: 9ms