Given an array of strings, group anagrams together.
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
] Note:
Solution 1:
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> anMap = new HashMap<String, List<String>>();
List<List<String>> retList = new ArrayList<List<String>>();
for(int i = 0; i<strs.length;i++){
char[] charArray = strs[i].toCharArray();
Arrays.sort(charArray);
String str = new String(charArray);
if(anMap.containsKey(str)){
List<String> innerList = anMap.get(str);
innerList.add(strs[i]);
anMap.put(str, innerList);
}else{
List<String> innerList = new ArrayList<String>();
innerList.add(strs[i]);
anMap.put(str, innerList);
}
}
for(String key:anMap.keySet()){
retList.add(anMap.get(key));
}
return retList;
}
} Question : Given an array of integers A, return the largest integer that only occurs once.…
Jump search algorithm is a pretty new algorithm to search for an element in a…
What is Knuth Morris Pratt or KMP algorithm ? KMP is an algorithm which is…
Binary Search is a Logarithmic search which finds the target element in a sorted array…
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X…
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such…
This website uses cookies.