Skip to content

Commit f9dc634

Browse files
committed
New Problem "Longest Absolute File Path"
1 parent 25633c1 commit f9dc634

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LeetCode
88

99
| # | Title | Solution | Difficulty |
1010
|---| ----- | -------- | ---------- |
11+
[388|[Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/) | [C++](./algorithms/cpp/longestAbsoluteFilePath/LongestAbsoluteFilePath.cpp)|Medium|
1112
|387|[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [C++](./algorithms/cpp/firstUniqueCharacterInAString/FirstUniqueCharacterInAString.cpp)|Easy|
1213
|386|[Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/) | [C++](./algorithms/cpp/lexicographicalNumbers/LexicographicalNumbers.cpp)|Medium|
1314
|377|[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/) | [C++](./algorithms/cpp/combinationSumIV/combinationSumIV.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Source : https://leetcode.com/problems/longest-absolute-file-path/
2+
// Author : Hao Chen
3+
// Date : 2016-08-23
4+
5+
/***************************************************************************************
6+
*
7+
* Suppose we abstract our file system by a string in the following manner:
8+
*
9+
* The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents:
10+
*
11+
* dir
12+
* subdir1
13+
* subdir2
14+
* file.ext
15+
*
16+
* The directory dir contains an empty sub-directory subdir1 and a sub-directory
17+
* subdir2 containing a file file.ext.
18+
*
19+
* The string
20+
* "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile
21+
* 2.ext" represents:
22+
*
23+
* dir
24+
* subdir1
25+
* file1.ext
26+
* subsubdir1
27+
* subdir2
28+
* subsubdir2
29+
* file2.ext
30+
*
31+
* The directory dir contains two sub-directories subdir1 and subdir2. subdir1 contains
32+
* a file file1.ext and an empty second-level sub-directory subsubdir1. subdir2
33+
* contains a second-level sub-directory subsubdir2 containing a file file2.ext.
34+
*
35+
* We are interested in finding the longest (number of characters) absolute path to a
36+
* file within our file system. For example, in the second example above, the longest
37+
* absolute path is "dir/subdir2/subsubdir2/file2.ext", and its length is 32 (not
38+
* including the double quotes).
39+
*
40+
* Given a string representing the file system in the above format, return the length
41+
* of the longest absolute path to file in the abstracted file system. If there is no
42+
* file in the system, return 0.
43+
*
44+
* Note:
45+
*
46+
* The name of a file contains at least a . and an extension.
47+
* The name of a directory or sub-directory will not contain a ..
48+
*
49+
* Time complexity required: O(n) where n is the size of the input string.
50+
*
51+
* Notice that a/aa/aaa/file1.txt is not the longest file path, if there is another
52+
* path aaaaaaaaaaaaaaaaaaaaa/sth.png.
53+
***************************************************************************************/
54+
55+
class Solution {
56+
public:
57+
// Solution
58+
// --------
59+
// We can see the input formation has the order
60+
// so, we can maintain an array which states the current level's path length
61+
//
62+
// For example:
63+
// dir
64+
// subdir1 <- length[ level1 = len("dir")+len("/"),
65+
// level2 = len("dir")+len("/")+len("subdir1")+len("/") ]
66+
// file.ext
67+
// subdir2
68+
// file.ext
69+
// subsubdir1 <- length[ level1 = len("dir")+len("/"),
70+
// level2 = len("dir")+len("/")+len("subdir2")+len("/"),
71+
// level3 = len("dir")+len("/")+len("subdir2")+len("/")+len("subsubdir1")+len("/") ]
72+
// file.ext
73+
//
74+
int lengthLongestPath(string input) {
75+
76+
stringstream ss(input);
77+
string line;
78+
int result = 0;
79+
80+
vector<int> length;
81+
length.push_back(0); //initialize top dummy level's length is zero
82+
83+
while (getline(ss, line, '\n')) {
84+
//get current level, start from 1
85+
int level = 0;
86+
while ( line[level++] == '\t'); // get the level
87+
int len = line.size() - level + 1;
88+
89+
//if is a file, then cacualte the total length.
90+
if (line.find('.') != string::npos) {
91+
if ( length[level-1] + len > result ) {
92+
result = length[level-1] + len;
93+
}
94+
} else {
95+
96+
if (length.size() <= level) {
97+
length.push_back(0);
98+
}
99+
100+
// if it a folder, then update the current level's length
101+
length[level] = length[level-1] + len + 1; // 1 for "/" path delimiter
102+
}
103+
104+
}
105+
return result;
106+
}
107+
};

0 commit comments

Comments
��(0)