Easy 242题 Valid Anagram

来源:互联网 发布:matalab y引入数据 编辑:程序博客网 时间:2024/05/01 05:47

Question:

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

For 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?


Solution:

public class Solution {    public boolean isAnagram(String s, String t) {        int slen=s.length();        int tlen=t.length();        if(slen!=tlen)            return false;        int[] map = new int[26];        for(int i=0;i<=slen-1;i++)            map[s.charAt(i)-'a']++;        for(int i=0;i<=tlen-1;i++)            map[t.charAt(i)-'a']--;        for(int i=0;i<=25;i++)            if(map[i]!=0)                return false;        return true;    }}



0 0
原创粉丝点击