一、 题目描述
二、解题思路
使用哈希表来实现。首先提取字符串中的内容,形成 的 。之后建立一个 为 类、 为 类型的 _ 。之所以采用 _ 是因为其内部实现为哈希表。遍历一遍 ,在创造 的过程中查看在 _ 里是否存在与 相等的 ,如果有,就把当前这个 的 插入到这个值对应的 里。
三、解题代码
class Solution {
private:
pair<string, string> CreateDocPair(string str, string rootPath){
int prior = 0;
for(; prior < str.size() && str[prior] != '('; prior++);
auto Name = rootPath + "/" + str.substr(0, prior);
auto Content = str.substr(prior + 1, str.size() - 1 - (prior + 1));
return make_pair(Content, Name);
}
public:
vector<vector<string>> findDuplicate(vector<string>& paths) {
unordered_map<string, vector<string>> mp;
vector<pair<string, string>> doc;
for(int i = 0; i < paths.size(); i++)
{
auto tmpCMD = paths[i];
int prior = 0;
for(; prior < tmpCMD.size() && tmpCMD[prior] != ' '; prior++);
auto rootPath = tmpCMD.substr(0, prior++);
for(; prior < tmpCMD.size(); prior++){
int ahead = prior;
for(; ahead < tmpCMD.size() && tmpCMD[ahead] != ' '; ahead++);
auto ret = CreateDocPair(tmpCMD.substr(prior, ahead - prior), rootPath);
prior = ahead;
mp[ret.first].push_back(ret.second);
}
}
vector<vector<string>> sln;
for(auto it: mp)
if(it.second.size() > 1) sln.push_back(it.second);
return sln;
}
};