diff --git a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README.md b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README.md index 113dbea8ab033..2066a078751e3 100644 --- a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README.md +++ b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README.md @@ -155,6 +155,24 @@ func removeSubfolders(folder []string) []string { } ``` +#### TypeScript + +```ts +function removeSubfolders(folder: string[]): string[] { + let s = folder[1]; + return folder.sort().filter(x => !x.startsWith(s + '/') && (s = x)); +} +``` + +#### JavaScript + +```js +function removeSubfolders(folder) { + let s = folder[1]; + return folder.sort().filter(x => !x.startsWith(s + '/') && (s = x)); +} +``` + @@ -379,6 +397,85 @@ func removeSubfolders(folder []string) []string { } ``` +#### TypeScript + +```ts +function removeSubfolders(folder: string[]): string[] { + const createTrie = (): T => ({ '#': false, children: {} }); + const trie = createTrie(); + + for (const f of folder) { + const path = f.split('/'); + path.shift(); + + let node = trie; + for (const p of path) { + if (!node.children[p]) node.children[p] = createTrie(); + node = node.children[p]; + } + node['#'] = true; + } + + const ans: string[] = []; + const dfs = (trie: T, path = '') => { + if (trie['#']) { + ans.push(path); + return; + } + + for (const key in trie.children) { + dfs(trie.children[key], path + '/' + key); + } + }; + + dfs(trie); + + return ans; +} + +type T = { + '#': boolean; + children: Record; +}; +``` + +#### JavaScript + +```js +function removeSubfolders(folder) { + const createTrie = () => ({ '#': false, children: {} }); + const trie = createTrie(); + + for (const f of folder) { + const path = f.split('/'); + path.shift(); + + let node = trie; + for (const p of path) { + if (!node.children[p]) node.children[p] = createTrie(); + node = node.children[p]; + } + node['#'] = true; + } + + const ans = []; + const dfs = (trie, path = '') => { + if (trie['#']) { + ans.push(path); + return; + } + + for (const key in trie.children) { + dfs(trie.children[key], path + '/' + key); + } + }; + + dfs(trie); + + return ans; +} +``` + diff --git a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README_EN.md b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README_EN.md index e26defdb14da8..6d9859eca427f 100644 --- a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README_EN.md +++ b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README_EN.md @@ -154,6 +154,24 @@ func removeSubfolders(folder []string) []string { } ``` +#### TypeScript + +```ts +function removeSubfolders(folder: string[]): string[] { + let s = folder[1]; + return folder.sort().filter(x => !x.startsWith(s + '/') && (s = x)); +} +``` + +#### JavaScript + +```js +function removeSubfolders(folder) { + let s = folder[1]; + return folder.sort().filter(x => !x.startsWith(s + '/') && (s = x)); +} +``` + @@ -378,6 +396,85 @@ func removeSubfolders(folder []string) []string { } ``` +#### TypeScript + +```ts +function removeSubfolders(folder: string[]): string[] { + const createTrie = (): T => ({ '#': false, children: {} }); + const trie = createTrie(); + + for (const f of folder) { + const path = f.split('/'); + path.shift(); + + let node = trie; + for (const p of path) { + if (!node.children[p]) node.children[p] = createTrie(); + node = node.children[p]; + } + node['#'] = true; + } + + const ans: string[] = []; + const dfs = (trie: T, path = '') => { + if (trie['#']) { + ans.push(path); + return; + } + + for (const key in trie.children) { + dfs(trie.children[key], path + '/' + key); + } + }; + + dfs(trie); + + return ans; +} + +type T = { + '#': boolean; + children: Record; +}; +``` + +#### JavaScript + +```js +function removeSubfolders(folder) { + const createTrie = () => ({ '#': false, children: {} }); + const trie = createTrie(); + + for (const f of folder) { + const path = f.split('/'); + path.shift(); + + let node = trie; + for (const p of path) { + if (!node.children[p]) node.children[p] = createTrie(); + node = node.children[p]; + } + node['#'] = true; + } + + const ans = []; + const dfs = (trie, path = '') => { + if (trie['#']) { + ans.push(path); + return; + } + + for (const key in trie.children) { + dfs(trie.children[key], path + '/' + key); + } + }; + + dfs(trie); + + return ans; +} +``` + diff --git a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution.js b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution.js new file mode 100644 index 0000000000000..9e81cc69ec857 --- /dev/null +++ b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution.js @@ -0,0 +1,4 @@ +function removeSubfolders(folder) { + let s = folder[1]; + return folder.sort().filter(x => !x.startsWith(s + '/') && (s = x)); +} diff --git a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution.ts b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution.ts new file mode 100644 index 0000000000000..1259848ef3f6e --- /dev/null +++ b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution.ts @@ -0,0 +1,4 @@ +function removeSubfolders(folder: string[]): string[] { + let s = folder[1]; + return folder.sort().filter(x => !x.startsWith(s + '/') && (s = x)); +} diff --git a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.js b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.js new file mode 100644 index 0000000000000..2c3e9e4bfca6e --- /dev/null +++ b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.js @@ -0,0 +1,32 @@ +function removeSubfolders(folder) { + const createTrie = () => ({ '#': false, children: {} }); + const trie = createTrie(); + + for (const f of folder) { + const path = f.split('/'); + path.shift(); + + let node = trie; + for (const p of path) { + if (!node.children[p]) node.children[p] = createTrie(); + node = node.children[p]; + } + node['#'] = true; + } + + const ans = []; + const dfs = (trie, path = '') => { + if (trie['#']) { + ans.push(path); + return; + } + + for (const key in trie.children) { + dfs(trie.children[key], path + '/' + key); + } + }; + + dfs(trie); + + return ans; +} diff --git a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.ts b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.ts new file mode 100644 index 0000000000000..07802228b1dc7 --- /dev/null +++ b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.ts @@ -0,0 +1,37 @@ +function removeSubfolders(folder: string[]): string[] { + const createTrie = (): T => ({ '#': false, children: {} }); + const trie = createTrie(); + + for (const f of folder) { + const path = f.split('/'); + path.shift(); + + let node = trie; + for (const p of path) { + if (!node.children[p]) node.children[p] = createTrie(); + node = node.children[p]; + } + node['#'] = true; + } + + const ans: string[] = []; + const dfs = (trie: T, path = '') => { + if (trie['#']) { + ans.push(path); + return; + } + + for (const key in trie.children) { + dfs(trie.children[key], path + '/' + key); + } + }; + + dfs(trie); + + return ans; +} + +type T = { + '#': boolean; + children: Record; +};