
示例
示例 1:
输入:paths = ["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)","root 4.txt(efgh)"]
输出:[["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]]
示例 2:
输入:paths = ["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)"]
输出:[["root/a/2.txt","root/c/d/4.txt"],["root/a/1.txt","root/c/3.txt"]]
解题




HashMap< String, List < String >> map = new HashMap < > ();
for (String path: paths) {
String[] values = path.split(" ");
for (int i = 1; i < values.length; i++) {
String[] name_cont = values[i].split("\(");
name_cont[1] = name_cont[1].replace(")", "");
List < String > list = map.getOrDefault(name_cont[1], new ArrayList < String > ());
list.add(values[0] + "/" + name_cont[0]);
map.put(name_cont[1], list);
}
}
List < List < String >> res = new ArrayList< >();
for (String key: map.keySet()) {
if (map.get(key).size() > 1)
res.add(map.get(key));
}
return res;
本篇文章来源于微信公众号:程序IT圈
原创文章,作者:栈长,如若转载,请注明出处:https://www.cxyquan.com/23155.html