您的位置:首页 > 职场人生

LeetCode之旅(13)-Valid Anagram

2016-03-30 18:45 369 查看

题目介绍:

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

思路分析:

题意是判断两个字符串包含的字符是不是相同,这样的问题,可以通过排序来简化 ,这个题目和前面的一个一片博客的题目(判断数组是否包含重复的元素)很相似,可以对照着

leetcode之旅(8)-Contains Duplicate

代码:

public class Solution {
public boolean isAnagram(String s, String t) {
char[] oneArray = s.toCharArray();
char[] twoArray = t.toCharArray();
Arrays.sort(oneArray);
Arrays.sort(twoArray);
int oneLength = oneArray.length;
int  twoLength = twoArray.length;
if(oneLength ==twoLength){
for(int i = 0;i < oneLength;i++){
if(oneArray[i] == twoArray[i]){

}else{
return false;
}
}
}else{
return false;
}
return true;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息