Skip to content

Commit

Permalink
Merge pull request #628 from 0xff-dev/388
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 388
  • Loading branch information
6boris authored Sep 24, 2023
2 parents ff2eabc + 6287bd2 commit 443059b
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 22 deletions.
61 changes: 48 additions & 13 deletions leetcode/301-400/0388.Longest-Absolute-File-Path/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,63 @@
# [388.Longest Absolute File Path][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture:

![1](./mdir.jpeg)

Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`. `subdir2` contains a subdirectory `subsubdir2`, which contains a file `file2.ext`.

**Example 1:**
In text form, it looks like this (with ⟶ representing the tab character):

```
Input: a = "11", b = "1"
Output: "100"
dir
⟶ subdir1
⟶ ⟶ file1.ext
⟶ ⟶ subsubdir1
⟶ subdir2
⟶ ⟶ subsubdir2
⟶ ⟶ ⟶ file2.ext
```

## 题意
> ...
If we were to write this representation in code, it will look like this: `"dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"`. Note that the `'\n'` and `'\t'` are the new-line and tab characters.

Every file and directory has a unique **absolute path** in the file system, which is the order of directories that must be opened to reach the file/directory itself, all concatenated by `'/'s`. Using the above example, the **absolute path** to `file2.ext` is `"dir/subdir2/subsubdir2/file2.ext"`. Each directory name consists of letters, digits, and/or spaces. Each file name is of the form `name.extension`, where `name` and `extension` consist of letters, digits, and/or spaces.

Given a string `input` representing the file system in the explained format, return the length of the **longest absolute path** to a **file** in the abstracted file system. If there is no file in the system, return `0`.

## 题解
**Note** that the testcases are generated such that the file system is valid and no file or directory name has length 0.

### 思路1
> ...
Longest Absolute File Path
```go

**Example 1:**

![example1](./dir1.jpeg)

```
Input: input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
Output: 20
Explanation: We have only one file, and the absolute path is "dir/subdir2/file.ext" of length 20.
```

**Example 2:**

![example2](./dir2.jpeg)

```
Input: input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
Output: 32
Explanation: We have two files:
"dir/subdir1/file1.ext" of length 21
"dir/subdir2/subsubdir2/file2.ext" of length 32.
We return 32 since it is the longest absolute path to a file.
```

**Example 3:**

```
Input: input = "a"
Output: 0
Explanation: We do not have any files, just a single directory named "a".
```

## 结语

Expand Down
93 changes: 91 additions & 2 deletions leetcode/301-400/0388.Longest-Absolute-File-Path/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,94 @@
package Solution

func Solution(x bool) bool {
return x
import "strings"

func Solution(input string) int {

ans := 0
if !strings.Contains(input, "\n\t") {
if !strings.Contains(input, ".") {
return ans
}

size := 0
for i := 0; i < len(input); i++ {
if input[i] == '\n' {
if size > ans {
ans = size
}
size = 0
continue
}
size++
}
if size > ans {
return size
}
return ans
}

var dfs func(string, string, *int, int) int
dfs = func(input, path string, index *int, tabs int) int {
if *index >= len(input) {
return tabs
}

l := len(input)
for {
// "a\n\tb.txt\na2\n\tb2.txt"
i := *index
// 读取一层dir
tab := 0
for ; i < len(input) && input[i] == '\t'; i++ {
tab++
}
if tab != tabs+1 {
return tab
}

size := 0
start := i
isEnd := false
for ; i < l && input[i] != '\n'; i++ {
size++
if input[i] == '.' {
isEnd = true
}
}
addPath := path + "/" + input[start:i]
*index = i + 1

if isEnd {
if r := len(addPath); r > ans {
ans = r
}
continue
}
needTabs := dfs(input, addPath, index, tab)
if needTabs != tabs+1 {
return needTabs
}
}
}

start, end := 0, 0
dirs := make([]string, 0)
for ; end < len(input); end++ {
if input[end] == '\n' {
if end < len(input)-1 && input[end+1] != '\t' {
dirs = append(dirs, input[start:end])
start = end + 1
}
}
}
dirs = append(dirs, input[start:end])
for _, baseInput := range dirs {
index := 0
for ; index < len(baseInput) && baseInput[index] != '\n'; index++ {
}
path := baseInput[:index]
index++
dfs(baseInput, path, &index, 0)
}
return ans
}
18 changes: 11 additions & 7 deletions leetcode/301-400/0388.Longest-Absolute-File-Path/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext", 20},
{"TestCase2", "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext", 32},
{"TestCase3", "a", 0},
{"TestCase4", "a.txt", 5},
{"TestCase5", "file1.txt\nfile2.txt\tlongfile.txt", 22},
{"TestCase6", "a\n\tb.txt\na2\n\tb2.txt", 9},
{"TestCase7", "a\n\taa\n\t\taaa\n\t\t\tfile1.txt\naaaaaaaaaaaaaaaaaaaaa\n\tsth.png", 29},
}

// 开始测试
Expand All @@ -30,10 +34,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 443059b

Please sign in to comment.