Valid Anagram【LeetCode】

来源:互联网 发布:安装java linux 编辑:程序博客网 时间:2024/06/05 14:17

题目:

Given two string 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 input contain unicode characters? How would you adapt your solution to such case?

思路分析:

判断连个字符串是不是相同字符,可以通过排序,判断排序之后的字符是否相等即可

代码:

public class Solution{    public boolean isAnagram(String s, String t{char[] oneArray = s.toCharArray();char[] twoArray = s.toCharArray();Arrays.sort(oneArray);Arrays.sort(twoArray);int oneLength = oneArray.length;int twoLength = twoArray.length;if(oneLength!=twoLength){return false;}else{for(int i=0;i<oneLength;i++){if(oneArray[i]!=twoArray[i]){return false;}elsereturn true;}}return false;}}
参考链接点击打开链接