本文主要是介绍LeetCode之旅(13)-Valid Anagram,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目介绍:
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;}
}
这篇关于LeetCode之旅(13)-Valid Anagram的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!