LeetCode-242. Valid Anagram

网友投稿 740 2022-08-25

LeetCode-242. Valid Anagram

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小时内删除侵权内容。

上一篇:LeetCode-105. Construct Binary Tree from Preorder and Inorder Traversal
下一篇:10个所需的IT技能 助你职场成功
相关文章

 发表评论

暂时没有评论,来抢沙发吧~