兩個字串是否是由相同字母(出現次數也相同)組成的
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?
Approach #1 (Sorting) [Accepted]
Algorithm
An anagram is produced by rearranging the letters of s into t. Therefore, if t is an anagram of s, sorting both strings will result in two identical strings. Furthermore, if s and t have different lengths, t must not be an anagram of s and we can return early.
public boolean isAnagram(String s, String t) { if (s.length() != t.length()) { //若長度不同則直接返回否 return false; } char[] str1 = s.toCharArray(); //String.toCharArray() 方法將字元串轉換為字元陣列 char[] str2 = t.toCharArray(); Arrays.sort(str1); //Arrays.sort() 需加包import java.util.*;或import java.util.ArraysArrays.sort(str2); return Arrays.equals(str1, str2); //Returns true if the two specified arrays of chars are equal to one another }
Complexity analysis
-
Time complexity : O(nlogn). Assume that n is the length of s, sorting costs O(nlogn) and comparing two strings costs O(n). Sorting time dominates and the overall time complexity is O(nlogn).
-
Space complexity : O(1). Space depends on the sorting implementation which, usually, costs O(1) auxiliary space if
heapsort
is used. Note that in Java,toCharArray()
makes a copy of the string so it costs O(n) extra space, but we ignore this for complexity analysis because:- It is a language dependent detail.
- It depends on how the function is designed. For example, the function parameter types can be changed to
char[]
.
pproach #2 (Hash Table) [Accepted]
Algorithm
To examine if t is a rearrangement of s, we can count occurrences of each letter in the two strings and compare them. Since both s and t contain only letters from a−z, a simple counter table of size 26 is suffice.
Do we need two counter tables for comparison? Actually no, because we could increment the counter for each letter in s and decrement the counter for each letter int, then check if the counter reaches back to zero.
public boolean isAnagram(String s, String t) { if (s.length() != t.length()) { return false; } int[] counter = new int[26]; for (int i = 0; i < s.length(); i++) { counter[s.charAt(i) - 'a']++; //Returns theOr we could first increment the counter for s, then decrement the counter for t. If at any point the counter drops below zero, we know that t contains an extra letter not in s and return false immediatelychar
value at the specified index. An index ranges from0
tolength() - 1
counter[t.charAt(i) - 'a']--; } for (int count : counter) { if (count != 0) { return false; } } return true; }
public boolean isAnagram(String s, String t) { if (s.length() != t.length()) { return false; } int[] table = new int[26]; for (int i = 0; i < s.length(); i++) { table[s.charAt(i) - 'a']++; } for (int i = 0; i < t.length(); i++) { table[t.charAt(i) - 'a']--; if (table[t.charAt(i) - 'a'] < 0) { return false; } } return true; }
Complexity analysis
-
Time complexity : O(n). Time complexity is O(n) because accessing the counter table is a constant time operation.
-
Space complexity : O(1). Although we do use extra space, the space complexity is O(1) because the table's size stays constant no matter how large n is.