zoukankan      html  css  js  c++  java
  • [Leetcode 54] 49 Anagrams

    Problem:

    Given an array of strings, return all groups of strings that are anagrams.

    Note: All inputs will be in lower-case.

    Analysis:

    anagrams are those strings that consist of same characters but in different order. A very simple way to solve the problem is try to find each string's anagrams. This requires O(n^2) by scanning through the string vector and comparing the two sorted string whether are the same. But it won't work if the geiven string vector is too big.

    A O(n) solution is as follows: use a map structure to keep the pair of <string, vector<string>>, the key is the sorted string the value is the original string's vector. In this way, we only need scan through the string vector and the map to find all anagrams. The complecity is only O(n+m), n is ths string number and m is the map entry number.

    Code:

    O(n^2) solution, Time Limit Execeed under larger test case

     1 class Solution {
     2 public:
     3     vector<string> anagrams(vector<string> &strs) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         int tag[strs.size()];
     7         vector<string> mid;
     8         vector<string> res;
     9         
    10         res.clear();
    11         for (int i=0; i<strs.size(); i++) {
    12             tag[i] = 0;
    13             mid.push_back(strs[i]);
    14             sort(mid[i].begin(), mid[i].end());
    15         }
    16             
    17         for (int i=0; i<strs.size(); i++) {
    18             bool find = false;
    19             if (tag[i] == 0) {
    20                 for (int j=i+1; j<strs.size(); j++) {
    21                     if (tag[j] == 0 && mid[i] == mid[j]) {
    22                             res.push_back(strs[j]);
    23                             find = true;
    24                             tag[j] = 1;
    25                     }
    26                 }
    27                 
    28             }
    29             
    30             if (find) {
    31                 tag[i] = 1;
    32                 res.push_back(strs[i]);
    33             }
    34         }
    35         
    36         return res;
    37     }
    38 };
    View Code

    O(n) solution

     1 class Solution {
     2 public:
     3     vector<string> anagrams(vector<string> &strs) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         vector<string> res;
     7         map<string, vector<string> > sm; 
     8         
     9         for (int i=0; i<strs.size(); i++) {
    10             string s = strs[i];
    11             sort(s.begin(), s.end());
    12             sm[s].push_back(strs[i]);
    13         }
    14         
    15         for (map<string, vector<string> >::iterator it=sm.begin(); 
    16                     it!=sm.end(); it++) {
    17             if ((*it).second.size() >= 2) {
    18                 for (int i=0; i<(*it).second.size(); i++) {
    19                     res.push_back((*it).second[i]);
    20                 }
    21             }
    22         }
    23         
    24         return res;
    25     }
    26 };
    View Code
  • 相关阅读:
    c#pc上测试微信端企业公众商城个人中心链接的工具JMeter
    eclipse java tomcat 远程调试
    Java多线程-线程池ThreadPoolExecutor构造方法和规则
    servlet3异步原理与实践
    eclipse Filter web.xml 问题解决 异步请求@WebServlet
    c# maiform父窗体改变动态的gridew 奇偶行变色的快捷方法
    java redispool测试类保存
    c#dev gridview 设置隔行换色等
    c#devexpress 窗体控件dock的重要
    devexpress总结 accordionControl 加载panelcontrol 的快捷方式
  • 原文地址:https://www.cnblogs.com/freeneng/p/3099602.html
Copyright © 2011-2022 走看看