-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path388.cpp
More file actions
executable file
·25 lines (25 loc) · 854 Bytes
/
388.cpp
File metadata and controls
executable file
·25 lines (25 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
int lengthLongestPath(string input) {
int res = 0, n = input.length(), level = 0;
unordered_map<int,int> m{{0,0}};
for (int i = 0; i < n; ++i) {
int start = i;
while (i < n && input[i] != '\n' && input[i] != '\t') ++i; // find the directory or file name
if (i == n || input[i] == '\n') {
string t = input.substr(start, i - start);
if (t.find('.') != string::npos) {
//file
res = max(res, m[level] + (int)t.size());
} else {
//directory
++level;
m[level] = m[level - 1] + (int)t.size() + 1;
}
level = 0;
}else
level++;
}
return res;
}
};