Leetcode 0242. Valid Anagram
Given two strings `s` and `t`, return `true` if `t` is an anagram of `s`, and `false` otherwise. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Description
Given two strings s
and t
, return true
if t
is an anagram of s
, and false
otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = “anagram”, t = “nagaram”
Output: true
Example 2:
Input: s = “rat”, t = “car”
Output: false
Constraints:
1 <= s.length, t.length <= 5 \* 10^4
s
andt
consist of lowercase English letters.
Follow up
- What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
/**
* Soring
* Analysis
* Time Complexity: BigO(n)
* Space Complexity: BigO(n)
*/
public boolean isAnagram(String s, String t) {
char sStr[] = s.toCharArray();
char tStr[] = t.toCharArray();
Arrays.sort(sStr);
Arrays.sort(tStr);
if (Arrays.equals(sStr, tStr)) return true;
return false;
}
}
This post is licensed under CC BY 4.0 by the author.