leetCode练习(242)
2017-03-03 10:06
323 查看
题目:Valid Anagram
难度:Easy
问题描述:
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.
解题思路:
将s和t转为数组,排序后比较是否相等即可。本题的测试条件为s和t同长不为null。
代码如下:
难度:Easy
问题描述:
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.
解题思路:
将s和t转为数组,排序后比较是否相等即可。本题的测试条件为s和t同长不为null。
代码如下:
public class Solution { public boolean isAnagram(String s, String t) { char[]stoc = s.toCharArray(); char[]ttoc = t.toCharArray(); Arrays.sort(ttoc); Arrays.sort(stoc); return Arrays.equals(ttoc, stoc)==true?true:false; } }
相关文章推荐
- leetCode练习(242)
- leetcode练习 Reconstruct Itinerary
- Leetcode242 Valid Anagram JAVA语言
- LeetCode Online Judge 题目C# 练习 - Longest Substring Without Repeating Characters
- LeetCode Online Judge 题目C# 练习 - Minimum Path Sum
- LeetCode Online Judge 题目C# 练习 - Partition List
- [LeetCode 242] Valid Anagram
- leetCode练习(81)
- leetcode练习
- LeetCode初级算法练习
- LeetCode Online Judge 题目C# 练习 - Plus One
- LeetCode Online Judge 题目C# 练习 - Rotate List
- LeetCode Online Judge 题目C# 练习 - Search a 2D Matrix
- LeetCode Online Judge 题目C# 练习 - Sparial Matrix II
- 『LeetCode』练习第三弹_算法5题
- LeetCode Online Judge 题目C# 练习 - Valid Number
- LeetCode 242 -Valid Anagram ( JAVA )
- 『LeetCode』练习第六弹_算法9题
- leetCode练习(20)
- leetcode练习题目