Leetcode 242.有效的字母异位词

有效的字母异位词

给定两个字符串 st ,编写一个函数来判断 t 是否是 s 的一个字母异位词。

示例 1:

输入: s = "anagram", t = "nagaram"

输出: true

示例 2:

输入: s = "rat", t = "car"

输出: false

说明:
你可以假设字符串只包含小写字母。

进阶:
如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?

 1 class Solution {
 2     public boolean isAnagram(String s, String t) {
 3         int[] count=new int[255];
 4         int sl=s.length();
 5         int tl=t.length();
 6         for(int i=0;i<sl;i++){
 7             count[s.charAt(i)]++;
 8         }
 9         for(int i=0;i<tl;i++){
10             count[t.charAt(i)]--;
11         }
12         for(int i=0;i<255;i++){
13             if(count[i]!=0) return false;
14         }
15         return true;
16     }
17 }

猜你喜欢

转载自www.cnblogs.com/kexinxin/p/10203119.html