242. Valid Anagram

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?

题目含义:判断给定两个字符串是否为相同字母不同排列的单词

 1     public boolean isAnagram(String s, String t) {
 2         if (s.length() != t.length()) return false;
 3         int[] letter=new int[26];
 4         for (int i=0;i<s.length();i++)
 5         {
 6             letter[s.charAt(i) - 'a']++;
 7         }
 8         for (int i=0;i<t.length();i++)
 9         {
10             if ((--letter[t.charAt(i)-'a'])<0) return false;
11         }
12         return true;        
13     }

 

posted @ 2017-10-23 21:05  daniel456  阅读(78)  评论(0编辑  收藏  举报