政务小程序如何重新定义政府服务与公众互动方式
724
2022-08-25
LeetCode-242. Valid Anagram
Given two strings s and t , write a function to determine if t is an anagram of s.
Example 1:
Input: s = "anagram", t = "nagaram"Output: true
Example 2:
Input: s = "rat", t = "car"Output: 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表即可。
class Solution {public: bool isAnagram(string s, string t) { int n = s.length(), m = t.length(); bool ans = true; if (n != m) { return false; } else if (n == 0) { return true; } int hash[26], cnt[26]; for (int i = 0; i < 26; i++) { hash[i] = 0; cnt[i] = 0; } for (int i = 0; i < n; i++) { hash[s[i] - 97]++; cnt[t[i] - 97]++; } for (int i = 0; i < 26; i++) { if (cnt[i] != hash[i]) { ans = false; break; } } return ans; }};
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~