leetcode242. 有效的字母异位词
【摘要】 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
示例 1:
输入: s = "anagram", t = "nagaram" 输出: true 示例 2:
输入: s = "rat", t = "car" 输出: false 说明: 你可以假设字符串只包含小写字母。
进阶: 如果输入字符串包含 unicode 字符怎么办?你...
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
示例 1:
输入: s = "anagram", t = "nagaram"
输出: true
示例 2:
输入: s = "rat", t = "car"
输出: false
说明:
你可以假设字符串只包含小写字母。
进阶:
如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?
多种思路不再介绍,很简单。
-
class Solution {
-
public boolean isAnagram(String s, String t) {
-
int[] sCounts = new int[26];
-
int[] tCounts = new int[26];
-
for (char ch : s.toCharArray()) {
-
sCounts[ch - 'a']++;
-
}
-
for (char ch : t.toCharArray()) {
-
tCounts[ch - 'a']++;
-
}
-
for (int i = 0; i < 26; i++) {
-
if (sCounts[i] != tCounts[i]) {
-
return false;
-
}
-
}
-
return true;
-
}
-
}
文章来源: fantianzuo.blog.csdn.net,作者:兔老大RabbitMQ,版权归原作者所有,如需转载,请联系作者。
原文链接:fantianzuo.blog.csdn.net/article/details/104143015
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)