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
  • 相关阅读:
    【转载】10个Web3D可视化精彩案例
    基于react的audio组件
    如何开发一款堪比APP的微信小程序(腾讯内部团队分享)
    CSS3 用border写 空心三角箭头 (两种写法)
    浅谈微信小程序对于创业者,意味着什么?
    左手Cookie“小甜饼”,右手Web Storage
    css3中user-select的用法详解
    个人感觉一些比较有用的特效例子
    纯css模拟电子钟
    蓝桥杯 ALGO-2:最大最小公倍数
  • 原文地址:https://www.cnblogs.com/freeneng/p/3099602.html
Copyright © 2011-2022 走看看