Leetcode - Valid Anagram

来源:互联网 发布:其他国家的顶级域名 编辑:程序博客网 时间:2024/06/06 11:38

Question

Given two strings s and t, write a function to determine if t is an anagram of s.


Example

s = “anagram”, t = “nagaram”, return true.
s = “rat”, t = “car”, return 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?


Java Code

//使用数组记录字符串中各个字符出现的次数,数组的索引对应字符的ASCII码public boolean isAnagram(String s, String t) {    int len = s.length();    if(len != t.length()) return false;    int[] sTable = new int[128];//默认初始化为0    int[] tTable = new int[128];    for(int i = 0; i < len; ++i) {        sTable[s.charAt(i)]++;        tTable[t.charAt(i)]++;    }    for(int i = 0; i < len; ++i) {        if(sTable[i] != tTable[i])            return false;    }    return true;}
0 0
原创粉丝点击