use map<key, val> in C++, solve LeetCode Anagrams 240 ms pass large test set
2013-04-21 16:22
531 查看
Anagrams
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
The basic idea is easy. From the definition of anagrams, we just need to get the histogram of each string. Then we just need to figure out if two string has the same histogram.
To calculate if two strings have the same histogram, we could use hashtable to do O(1) comparison, or use tree structure to do O(logN)
Now let's suppose we use the map in C++ STL to do O(logN) to compare the key. The key is actually an array of 26 numbers (each number indicate how many times a word appears in the string). To use map, we need to create a comparison method, we could do
class HistKey{
};
class MyComp{
bool operator(const HistKey &a, const HistKey &b){
//if a < b, return true;
//else return false; ********************* It is very important here that we need to return false if a==b
}
};
we could create the map by
map<HistKey, pair<string, bool>> histogram
To find if two histogram is the same for two strings, we could just do:
histogram.find(a_hist) != histogram.end()
Note, it is important that if two key are the same, it need to return false in MyComp
My code could pass the result within 240 ms. Please let me know if you would like to see the code
Reference:
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
The basic idea is easy. From the definition of anagrams, we just need to get the histogram of each string. Then we just need to figure out if two string has the same histogram.
To calculate if two strings have the same histogram, we could use hashtable to do O(1) comparison, or use tree structure to do O(logN)
Now let's suppose we use the map in C++ STL to do O(logN) to compare the key. The key is actually an array of 26 numbers (each number indicate how many times a word appears in the string). To use map, we need to create a comparison method, we could do
class HistKey{
};
class MyComp{
bool operator(const HistKey &a, const HistKey &b){
//if a < b, return true;
//else return false; ********************* It is very important here that we need to return false if a==b
}
};
we could create the map by
map<HistKey, pair<string, bool>> histogram
To find if two histogram is the same for two strings, we could just do:
histogram.find(a_hist) != histogram.end()
Note, it is important that if two key are the same, it need to return false in MyComp
My code could pass the result within 240 ms. Please let me know if you would like to see the code
Reference:
Map以自定义类做为键值
http://www.seacha.com/article.php/knowledge/cbase/2011/1020/1859.html相关文章推荐
- How to sort a Map<Key, Value> on the values in Java?
- <LeetCode OJ> Find Minimum in Rotated Sorted Array【153】
- Sort a Map<Key, Value> by values (Java)
- Address already in use: JVM_Bind<null>:8080
- Tomcat端口被占用Address already in use: JVM_Bind <null>:8080
- JSP标签的<jsp:useBean>和<jsp:setProperty><jsp:getProperty>的使用
- Address already in use: JVM_Bind<null>:8080
- java.net.BindException: Address already in use <null>:8081
- tomcat端口冲突解决 Address already in use: JVM_Bind <null>:8080
- summary in July <test Nahanni shared memory>
- 集合转换java:【List<-->数组、List<-->Set、数组<-->Set、Map-->Set、Map-->List】
- <LeetCode OJ> 215. Kth Largest Element in an Array
- List<Map>集合根据Map里的两个key值对List集合重新排序
- Hibernate Cannot use identity column key generation with <union-subclass> mapping for
- Internal navigation rejected: <allow-navigation not set> in Cordova on iOS
- List<Map>集合根据Map里某个key对List集合重新排序介绍
- Map集合中Set<Entry<Character,Integer>>entrySet=map.entrySet();
- Error: must set key for <rc-animate> children
- The method setClass(Context, Class<?>) in the type Intent is not applicable for the问题
- <LeetCode OJ> 230. Kth Smallest Element in a BST