Skip to content

Latest commit

 

History

History
45 lines (39 loc) · 1.02 KB

1233.删除子文件夹.md

File metadata and controls

45 lines (39 loc) · 1.02 KB

1233.删除子文件夹

/*
 * @lc app=leetcode.cn id=1233 lang=typescript
 *
 * [1233] 删除子文件夹
 */

// @lc code=start
function removeSubfolders(folder: string[]): string[] {}
// @lc code=end

解法 1: 哈希表

function removeSubfolders(folder: string[]): string[] {
  const set = new Set(folder)
  const res: string[] = []
  next: for (const path of folder) {
    const names = path.split('/')
    let p = '/'
    for (let i = 1; i < names.length - 1; i++) {
      p += names[i]
      if (set.has(p)) continue next
      p += '/'
    }
    res.push(path)
  }
  return res
}

Case

test.each([
  { input: { folder: ['/a', '/a/b', '/c/d', '/c/d/e', '/c/f'] }, output: ['/a', '/c/d', '/c/f'] },
  { input: { folder: ['/a', '/a/b/c', '/a/b/d'] }, output: ['/a'] },
  { input: { folder: ['/a/b/c', '/a/b/ca', '/a/b/d'] }, output: ['/a/b/c', '/a/b/ca', '/a/b/d'] },
])('input: folder = $input.folder', ({ input: { folder }, output }) => {
  expect(removeSubfolders(folder)).toEqual(output)
})