Anagram总结
有關異構詞的題目,考察我們對字符串的處理能力,這里列舉leetcode兩道關于異構詞的題目。
[b]1,Valid Anagram[/b]
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.
題目的意思是給定兩個字符串,判斷它們是否是異構詞。
首先我們想到的方法是將字符串轉換成字符數組,然后排序之后比較,如果兩個新的字符串相同就返回true; 否則返回false;代碼如下:
public class Solution {
public boolean isAnagram(String s, String t) {
char[] s1 = s.toCharArray();
char[] t1 = t.toCharArray();
Arrays.sort(s1);
Arrays.sort(t1);
s = new String(s1);
t = new String(t1);
if(s.equals(t)) return true;
return false;
}
}
我們也可以采用一種類似于計數排序的方法來處理這個問題,先將一個字符串中的每個字符按照它們對應的不同的值來存入數組中,然后用第二個字符串中每個字符對應的值減去依次相減,如果數組中有的值不為零就返回false,如果全為0就返回true。代碼如下:
public class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;
int[] count = new int [128];
for (int i = 0; i < s.length(); i++)
count[s.charAt(i) - '0'] ++;
for (int j = 0; j < t.length(); j++)
count[t.charAt(j) - '0'] --;
for(int i = 0; i < count.length; i++) {
if(count[i] != 0)
return false;
}
return true;
}
}
[b]2,Group Anagrams[/b]
Given an array of strings, group anagrams together.
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
Return:
[
["ate", "eat","tea"],
["nat","tan"],
["bat"]
]
Note:
For the return value, each inner list's elements must follow the lexicographic order.
All inputs will be in lower-case.
題目的意思是給定一個字符串數組,將異構詞分組,然后輸出。我們借助哈希表,方法類似于第一題中的第一個方法,先排序,然后存放在哈希表中的key中,代碼如下:
public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<String, List<String>> hm = new HashMap<String, List<String>>();
for(int i = 0; i < strs.length; i++) {
char[] temp = strs[i].toCharArray();
Arrays.sort(temp);
String s = new String(temp);
if(!hm.containsKey(s)) {
hm.put(s, new ArrayList<String>());
}
hm.get(s).add(strs[i]);
}
for(List<String> vals : hm.values()) {
Collections.sort(vals);
}
return new ArrayList<List<String>>(hm.values());
}
}
[b]1,Valid Anagram[/b]
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.
題目的意思是給定兩個字符串,判斷它們是否是異構詞。
首先我們想到的方法是將字符串轉換成字符數組,然后排序之后比較,如果兩個新的字符串相同就返回true; 否則返回false;代碼如下:
public class Solution {
public boolean isAnagram(String s, String t) {
char[] s1 = s.toCharArray();
char[] t1 = t.toCharArray();
Arrays.sort(s1);
Arrays.sort(t1);
s = new String(s1);
t = new String(t1);
if(s.equals(t)) return true;
return false;
}
}
我們也可以采用一種類似于計數排序的方法來處理這個問題,先將一個字符串中的每個字符按照它們對應的不同的值來存入數組中,然后用第二個字符串中每個字符對應的值減去依次相減,如果數組中有的值不為零就返回false,如果全為0就返回true。代碼如下:
public class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;
int[] count = new int [128];
for (int i = 0; i < s.length(); i++)
count[s.charAt(i) - '0'] ++;
for (int j = 0; j < t.length(); j++)
count[t.charAt(j) - '0'] --;
for(int i = 0; i < count.length; i++) {
if(count[i] != 0)
return false;
}
return true;
}
}
[b]2,Group Anagrams[/b]
Given an array of strings, group anagrams together.
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
Return:
[
["ate", "eat","tea"],
["nat","tan"],
["bat"]
]
Note:
For the return value, each inner list's elements must follow the lexicographic order.
All inputs will be in lower-case.
題目的意思是給定一個字符串數組,將異構詞分組,然后輸出。我們借助哈希表,方法類似于第一題中的第一個方法,先排序,然后存放在哈希表中的key中,代碼如下:
public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<String, List<String>> hm = new HashMap<String, List<String>>();
for(int i = 0; i < strs.length; i++) {
char[] temp = strs[i].toCharArray();
Arrays.sort(temp);
String s = new String(temp);
if(!hm.containsKey(s)) {
hm.put(s, new ArrayList<String>());
}
hm.get(s).add(strs[i]);
}
for(List<String> vals : hm.values()) {
Collections.sort(vals);
}
return new ArrayList<List<String>>(hm.values());
}
}
總結
- 上一篇: hdu5745 La Vie en ro
- 下一篇: [附源码]Python计算机毕业设计Dj