首页 > 其他 > 详细

Leetcode: Valid Anagram

时间:2015-12-20 14:31:48      阅读:229      评论: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?

还不知道Unicode 该怎么做

只有lowercase alphabets的做法无外乎sort, hashmap, array, bitmap之类的

 1 public class Solution {
 2     public boolean isAnagram(String s, String t) {
 3         if (s==null || t==null || s.length()!=t.length()) return false;
 4         char[] ss = s.toCharArray();
 5         char[] tt = t.toCharArray();
 6         Arrays.sort(ss);
 7         Arrays.sort(tt);
 8         for (int i=0; i<ss.length; i++) {
 9             if (ss[i] != tt[i]) return false;
10         }
11         return true;
12     }
13 }

 

Leetcode: Valid Anagram

原文:http://www.cnblogs.com/EdwardLiu/p/5060721.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!