码迷,mamicode.com
首页 > 其他好文 > 详细

242. Valid Anagram

时间:2016-04-12 19:03:10      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

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?

 

Subscribe to see which companies asked this question

由题 只有小写字母,使用26位的数组存储每个字母个数进行比较。

 1 public class Solution {
 2     public boolean isAnagram(String s, String t) {
 3         if(s == null || t == null || s.length() != t.length())
 4             return false;
 5         int[] cmap = new int[26];
 6         for(int i=0;i<s.length();i++){
 7             cmap[s.charAt(i)-‘a‘]++;
 8             cmap[t.charAt(i)-‘a‘]--;
 9         }
10         for(int c : cmap){
11             if(c != 0)
12                 return false;
13         }
14         return true;
15     }
16 }

 

242. Valid Anagram

标签:

原文地址:http://www.cnblogs.com/guoguolan/p/5383587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!