aboutsummaryrefslogtreecommitdiff
path: root/find_duplicate_file.cpp
blob: 17ff6f0bc15e10d61a32b8f7a767ec54751d541d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "leetcode.h"

class Solution {
public:
  vvd(string) findDuplicate(vector<string> &paths) {
    unordered_map<string, vector<string>> files;
    vvd(string) ans;
    for (auto &path : paths) {
      int i = 0, j;
      while (path[i] != ' ')
        i++;
      string dir = path.substr(0, i), file, content;
      while (i++ < path.size()) {
        j = i;
        while (path[i] != '(')
          i++;
        file = path.substr(j, i - j);
        j = i;
        while (path[i++] != ')')
          ;
        content = path.substr(j, i - j);
        files[content].emplace_back(dir + '/' + file);
      }
    }
    for (auto &content : files)
      if (content.second.size() > 1)
        ans.push_back(move(content.second));
    return ans;
  }
};