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
  • 相关阅读:
    mpvue: stylus定义主题色
    mpvue: 单个页面自定义导航栏或隐藏
    mpvue: fsevents is not a constructor
    Nginx:taro h5 Nginx代理配置
    git: 替换remote origin
    taro3.x: h5地图兼容组件封装
    taro3.x: h5地图兼容
    taro3.x: 使用taro-router在H5浏览器返回报错
    taro3.x: tarojs-router
    java线程与线程安全的单例模式
  • 原文地址:https://www.cnblogs.com/freeneng/p/3099602.html
Copyright © 2011-2022 走看看