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

Leetcode: Valid Anagram

时间:2015-12-20 14:32:37      阅读:180      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!