-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
feat: add solutions to lc problem: No.1948 #4580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
bac33bb
Update README.md
samarthswami1016 4f2027d
Update README_EN.md
samarthswami1016 44776a5
Add files via upload
samarthswami1016 cecfdab
style: format code and docs with prettier
samarthswami1016 202e120
Update README.md
samarthswami1016 cb36982
Update README_EN.md
samarthswami1016 c26dddb
Merge branch 'main' into main
samarthswami1016 8637c02
Merge branch 'main' into main
samarthswami1016 5890b5c
Merge branch 'doocs:main' into main
samarthswami1016 0c9a8c9
Push
samarthswami1016 897d9ef
style: format code and docs with prettier
samarthswami1016 51e99d9
Merge branch 'doocs:main' into main
samarthswami1016 10d7d3a
Merge branch 'doocs:main' into main
samarthswami1016 8c56e29
feat: add solutions to lc problem: No.3500
samarthswami1016 09668b7
style: format code and docs with prettier
samarthswami1016 df3cc36
Merge branch 'doocs:main' into main
samarthswami1016 63cbebd
Merge branch 'doocs:main' into main
samarthswami1016 6ee2647
Merge branch 'doocs:main' into main
samarthswami1016 3b925b4
feat: add solutions to lc problem: No.1948
samarthswami1016 84ae946
Merge branch 'doocs:main' into main
samarthswami1016 78a3359
style: format code and docs with prettier
samarthswami1016 9862d73
feat: add solutions to lc problem: No.1948
samarthswami1016 c1281dc
Merge branch 'main' into main
samarthswami1016 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
498 changes: 341 additions & 157 deletions
solution/1900-1999/1948.Delete Duplicate Folders in System/README.md
Oops, something went wrong.
472 changes: 328 additions & 144 deletions
solution/1900-1999/1948.Delete Duplicate Folders in System/README_EN.md
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
solution/1900-1999/1948.Delete Duplicate Folders in System/solution.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
struct Trie { | ||
string serial; | ||
unordered_map<string, Trie*> children; | ||
}; | ||
|
||
class Solution { | ||
public: | ||
vector<vector<string>> deleteDuplicateFolder( | ||
vector<vector<string>>& paths) { | ||
Trie* root = new Trie(); | ||
|
||
for (const vector<string>& path : paths) { | ||
Trie* cur = root; | ||
for (const string& node : path) { | ||
if (!cur->children.count(node)) { | ||
cur->children[node] = new Trie(); | ||
} | ||
cur = cur->children[node]; | ||
} | ||
} | ||
unordered_map<string, int> freq; | ||
function<void(Trie*)> construct = [&](Trie* node) { | ||
if (node->children.empty()) { | ||
return; | ||
} | ||
|
||
vector<string> v; | ||
for (const auto& [folder, child] : node->children) { | ||
construct(child); | ||
v.push_back(folder + "(" + child->serial + ")"); | ||
} | ||
sort(v.begin(), v.end()); | ||
for (string& s : v) { | ||
node->serial += move(s); | ||
} | ||
++freq[node->serial]; | ||
}; | ||
|
||
construct(root); | ||
|
||
vector<vector<string>> ans; | ||
vector<string> path; | ||
|
||
function<void(Trie*)> operate = [&](Trie* node) { | ||
if (freq[node->serial] > 1) { | ||
return; | ||
} | ||
if (!path.empty()) { | ||
ans.push_back(path); | ||
} | ||
for (const auto& [folder, child] : node->children) { | ||
path.push_back(folder); | ||
operate(child); | ||
path.pop_back(); | ||
} | ||
}; | ||
|
||
operate(root); | ||
return ans; | ||
} | ||
}; |
56 changes: 56 additions & 0 deletions
solution/1900-1999/1948.Delete Duplicate Folders in System/solution.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
type Trie struct { | ||
serial string | ||
children map[string]*Trie | ||
} | ||
|
||
func deleteDuplicateFolder(paths [][]string) [][]string { | ||
root := &Trie{children: make(map[string]*Trie)} | ||
for _, path := range paths { | ||
cur := root | ||
for _, node := range path { | ||
if _, ok := cur.children[node]; !ok { | ||
cur.children[node] = &Trie{children: make(map[string]*Trie)} | ||
} | ||
cur = cur.children[node] | ||
} | ||
} | ||
|
||
freq := make(map[string]int) | ||
var construct func(*Trie) | ||
construct = func(node *Trie) { | ||
if len(node.children) == 0 { | ||
return | ||
} | ||
v := make([]string, 0, len(node.children)) | ||
for folder, child := range node.children { | ||
construct(child) | ||
v = append(v, folder+"("+child.serial+")") | ||
} | ||
sort.Strings(v) | ||
node.serial = strings.Join(v, "") | ||
freq[node.serial]++ | ||
} | ||
construct(root) | ||
|
||
ans := make([][]string, 0) | ||
path := make([]string, 0) | ||
var operate func(*Trie) | ||
operate = func(node *Trie) { | ||
if freq[node.serial] > 1 { | ||
return | ||
} | ||
if len(path) > 0 { | ||
tmp := make([]string, len(path)) | ||
copy(tmp, path) | ||
ans = append(ans, tmp) | ||
} | ||
for folder, child := range node.children { | ||
path = append(path, folder) | ||
operate(child) | ||
path = path[:len(path)-1] | ||
} | ||
} | ||
operate(root) | ||
|
||
return ans | ||
} |
63 changes: 63 additions & 0 deletions
solution/1900-1999/1948.Delete Duplicate Folders in System/solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
class Solution { | ||
|
||
class Trie { | ||
|
||
String serial; | ||
Map<String, Trie> children = new HashMap<>(); | ||
} | ||
|
||
public List<List<String>> deleteDuplicateFolder(List<List<String>> paths) { | ||
Trie root = new Trie(); | ||
for (List<String> path : paths) { | ||
Trie cur = root; | ||
for (String node : path) { | ||
cur.children.putIfAbsent(node, new Trie()); | ||
cur = cur.children.get(node); | ||
} | ||
} | ||
|
||
Map<String, Integer> freq = new HashMap<>(); | ||
construct(root, freq); | ||
List<List<String>> ans = new ArrayList<>(); | ||
List<String> path = new ArrayList<>(); | ||
operate(root, freq, path, ans); | ||
return ans; | ||
} | ||
|
||
private void construct(Trie node, Map<String, Integer> freq) { | ||
if (node.children.isEmpty()) return; | ||
|
||
List<String> v = new ArrayList<>(); | ||
for (Map.Entry<String, Trie> entry : node.children.entrySet()) { | ||
construct(entry.getValue(), freq); | ||
v.add(entry.getKey() + "(" + entry.getValue().serial + ")"); | ||
} | ||
|
||
Collections.sort(v); | ||
StringBuilder sb = new StringBuilder(); | ||
for (String s : v) { | ||
sb.append(s); | ||
} | ||
node.serial = sb.toString(); | ||
freq.put(node.serial, freq.getOrDefault(node.serial, 0) + 1); | ||
} | ||
|
||
private void operate( | ||
Trie node, | ||
Map<String, Integer> freq, | ||
List<String> path, | ||
List<List<String>> ans | ||
) { | ||
if (freq.getOrDefault(node.serial, 0) > 1) return; | ||
|
||
if (!path.isEmpty()) { | ||
ans.add(new ArrayList<>(path)); | ||
} | ||
|
||
for (Map.Entry<String, Trie> entry : node.children.entrySet()) { | ||
path.add(entry.getKey()); | ||
operate(entry.getValue(), freq, path, ans); | ||
path.remove(path.size() - 1); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
solution/1900-1999/1948.Delete Duplicate Folders in System/solution.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
var deleteDuplicateFolder = function (paths) { | ||
class Trie { | ||
constructor() { | ||
this.serial = ''; | ||
this.children = new Map(); | ||
} | ||
} | ||
|
||
const root = new Trie(); | ||
for (const path of paths) { | ||
let cur = root; | ||
for (const node of path) { | ||
if (!cur.children.has(node)) { | ||
cur.children.set(node, new Trie()); | ||
} | ||
cur = cur.children.get(node); | ||
} | ||
} | ||
|
||
const freq = new Map(); | ||
function construct(node) { | ||
if (node.children.size === 0) return; | ||
const v = []; | ||
for (const [folder, child] of node.children) { | ||
construct(child); | ||
v.push(`${folder}(${child.serial})`); | ||
} | ||
v.sort(); | ||
node.serial = v.join(''); | ||
freq.set(node.serial, (freq.get(node.serial) || 0) + 1); | ||
} | ||
construct(root); | ||
|
||
const ans = []; | ||
const path = []; | ||
function operate(node) { | ||
if ((freq.get(node.serial) || 0) > 1) return; | ||
if (path.length > 0) { | ||
ans.push([...path]); | ||
} | ||
for (const [folder, child] of node.children) { | ||
path.push(folder); | ||
operate(child); | ||
path.pop(); | ||
} | ||
} | ||
operate(root); | ||
|
||
return ans; | ||
}; |
42 changes: 42 additions & 0 deletions
solution/1900-1999/1948.Delete Duplicate Folders in System/solution.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class Trie: | ||
serial: str = "" | ||
children: dict | ||
|
||
def __init__(self): | ||
self.children = dict() | ||
|
||
|
||
class Solution: | ||
def deleteDuplicateFolder(self, paths: List[List[str]]) -> List[List[str]]: | ||
root = Trie() | ||
for path in paths: | ||
cur = root | ||
for node in path: | ||
if node not in cur.children: | ||
cur.children[node] = Trie() | ||
cur = cur.children[node] | ||
freq = Counter() | ||
def construct(node: Trie) -> None: | ||
if not node.children: | ||
return | ||
v = list() | ||
for folder, child in node.children.items(): | ||
construct(child) | ||
v.append(folder + "(" + child.serial + ")") | ||
v.sort() | ||
node.serial = "".join(v) | ||
freq[node.serial] += 1 | ||
construct(root) | ||
ans = list() | ||
path = list() | ||
def operate(node: Trie) -> None: | ||
if freq[node.serial] > 1: | ||
return | ||
if path: | ||
ans.append(path[:]) | ||
for folder, child in node.children.items(): | ||
path.append(folder) | ||
operate(child) | ||
path.pop() | ||
operate(root) | ||
return ans |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.