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.
[ "anagram", "nagaram" ]
Explanation. nagaram is formed by rearranging the letters of anagram.
[ "rat", "car" ]
Explanation. rat and car do not contain the same characters in the same counts.
[ "listen", "silentt" ]
Explanation. The strings have different lengths, so they cannot be anagrams.
[ "a", "a" ]
Explanation. A single character string is an anagram of itself.
[ "aabbc", "abcab" ]
Explanation. Both strings have two 'a's, two 'b's, and one 'c'.
[ "aabb", "bbaa" ]
Explanation. aabb is an anagram of bbaa, as both contain two 'a's and two 'b's.
[ "ab", "ba" ]
Explanation. ab is an anagram of ba.
[ "abacaba", "aabbaac" ]
Explanation. Counts of characters don't match: 'b' count is 2 in s, 2 in t. 'c' count is 1 in s, 1 in t. 'a' count is 4 in s, 4 in t. This is true. Let's make it more distinct. s: 'abacaba', t: 'aababca'. s = {a:4, b:2, c:1}. t = {a:4, b:2, c:1}. This is true. Let's make it different. s="aabbc", t="abbcc"
[ "aabbc", "abbcc" ]
Explanation. String s has two 'a's, two 'b's, and one 'c'. String t has one 'a', two 'b's, and two 'c's. The character counts do not match.
Follow-up: What if the input strings contain Unicode characters? How would you adapt your solution?
1 <= s.length, t.length <= 5 * 10^4.s and t consist of lowercase English letters.
- Views
- 2