您的位置:首页 > 其它

两个字符串是否是由相同字母(出现次数也相同)组成的

2016-12-15 19:15 246 查看
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 ss into tt.
Therefore, if tt is
an anagram of ss,
sorting both strings will result in two identical strings. Furthermore, if ss and tt have
different lengths, tt must
not be an anagram of ss and
we can return early.
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
4000
//若长度不同则直接返回否
return false;
}
char[] str1 = s.toCharArray();         //String.toCharArray() 方法将字符串转换为字符数组
char[] str2 = t.toCharArray();
Arrays.sort(str1);                     //Arrays.sort() 需加包import java.util.*;或import java.util.Arrays
Arrays.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(n
\log n)O(nlogn).
Assume that nn is
the length of ss,
sorting costs O(n
\log n)O(nlogn) and
comparing two strings costs O(n)O(n).
Sorting time dominates and the overall time complexity is O(n
\log n)O(nlogn).

Space complexity : O(1)O(1).
Space depends on the sorting implementation which, usually, costs O(1)O(1) auxiliary
space if 
heapsort
 is used. Note that in Java,
toCharArray()
 makes
a copy of the string so it costs O(n)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 tt is
a rearrangement of ss,
we can count occurrences of each letter in the two strings and compare them. Since both ss and tt contain
only letters from a-za−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 ss and
decrement the counter for each letter intt,
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 the [code]char
 value at the specified index. An index ranges from 
0
 to 
length() - 1

counter[t.charAt(i) - 'a']--;
}
for (int count : counter) {
if (count != 0) {
return false;
}
}
return true;
}[/code]

Or we could first increment the counter for ss,
then decrement the counter for tt.
If at any point the counter drops below zero, we know that tt contains
an extra letter not in ss and
return false immediately 

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)O(n).
Time complexity is O(n)O(n) because
accessing the counter table is a constant time operation.

Space complexity : O(1)O(1).
Although we do use extra space, the space complexity is O(1)O(1) because
the table's size stays constant no matter how large nn is.

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐