当前位置:网站首页>Stack Title: the longest absolute path of a file
Stack Title: the longest absolute path of a file
2022-07-26 02:24:00 【Great Cherny】
List of articles
subject
Title and provenance
title : The longest absolute path to the file
Source :388. The longest absolute path to the file
difficulty
7 level
Title Description
requirement
Suppose there is a file system to store files and directories . An example of a file system is shown in the following figure :

There will be dir \texttt{dir} dir As the only directory in the root directory . dir \texttt{dir} dir Contains two subdirectories subdir1 \texttt{subdir1} subdir1 and subdir2 \texttt{subdir2} subdir2. subdir1 \texttt{subdir1} subdir1 Include files file1.ext \texttt{file1.ext} file1.ext And subdirectories subsubdir1 \texttt{subsubdir1} subsubdir1; subdir2 \texttt{subdir2} subdir2 Include subdirectories subsubdir2 \texttt{subsubdir2} subsubdir2, This subdirectory contains files file2.ext \texttt{file2.ext} file2.ext.
In text format , As shown below (* Represents a tab ):
dir
* subdir1
* * file1.ext
* * subsubdir1
* subdir2
* * subsubdir2
* * * file2.ext
In case of code representation , The above file system can be written as "dir \ n \ tsubdir1 \ n \ tfile1.ext \ n \ t \ tsubsubdir1 \ n \ tsubdir2 \ n \ t \ tsubsubdir2 \ n \ t \ t \ tfile2.ext" \texttt{"dir}\backslash\texttt{n}\backslash\texttt{tsubdir1}\backslash\texttt{n}\backslash\texttt{tfile1.ext}\backslash\texttt{n}\backslash\texttt{t}\backslash\texttt{tsubsubdir1}\backslash\texttt{n}\backslash\texttt{tsubdir2}\backslash\texttt{n}\backslash\texttt{t}\backslash\texttt{tsubsubdir2}\backslash\texttt{n}\backslash\texttt{t}\backslash\texttt{t}\backslash\texttt{tfile2.ext"} "dir\n\tsubdir1\n\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext". among ‘ \ n’ \texttt{`}\backslash\texttt{n'} ‘\n’ and ‘ \ t’ \texttt{`}\backslash\texttt{t'} ‘\t’ Line breaks and tabs, respectively .
Each file and folder in the file system has a unique Absolute path , That is, you must open it to reach the file / Directory order of directory location , For all paths ‘/’ \texttt{`/'} ‘/’ Connect . In the example above , Point to file2.ext \texttt{file2.ext} file2.ext The absolute path of "dir/subdir2/subsubdir2/file2.ext" \texttt{"dir/subdir2/subsubdir2/file2.ext"} "dir/subdir2/subsubdir2/file2.ext". Each directory name consists of the letters 、 Numbers and / Or spaces , Each file name follows name.extension \texttt{name.extension} name.extension The format of , Where the name and extension consist of the letters 、 Numbers and / Or spaces .
Give a string representing the file system in the above format input \texttt{input} input, Return to the file system The longest absolute path to a file The length of . If there are no files in the system , return 0 \texttt{0} 0.
Example
Example 1:

Input : input = "dir \ n \ tsubdir1 \ n \ tsubdir2 \ n \ t \ tfile.ext" \texttt{input = "dir}\backslash\texttt{n}\backslash\texttt{tsubdir1}\backslash\texttt{n}\backslash\texttt{tsubdir2}\backslash\texttt{n}\backslash\texttt{t}\backslash\texttt{tfile.ext"} input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
Output : 20 \texttt{20} 20
explain : Only one file , The absolute path is "dir/subdir2/file.ext" \texttt{"dir/subdir2/file.ext"} "dir/subdir2/file.ext", The length of the path 20 \texttt{20} 20.
Example 2:

Input : input = "dir \ n \ tsubdir1 \ n \ t \ tfile1.ext \ n \ t \ tsubsubdir1 \ n \ tsubdir2 \ n \ t \ tsubsubdir2 \ n \ t \ t \ tfile2.ext" \texttt{input = "dir}\backslash\texttt{n}\backslash\texttt{tsubdir1}\backslash\texttt{n}\backslash\texttt{t}\backslash\texttt{tfile1.ext}\backslash\texttt{n}\backslash\texttt{t}\backslash\texttt{tsubsubdir1}\backslash\texttt{n}\backslash\texttt{tsubdir2}\backslash\texttt{n}\backslash\texttt{t}\backslash\texttt{tsubsubdir2}\backslash\texttt{n}\backslash\texttt{t}\backslash\texttt{t}\backslash\texttt{tfile2.ext"} input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
Output : 32 \texttt{32} 32
explain : There are two files :
"dir/subdir1/file1.ext" \texttt{"dir/subdir1/file1.ext"} "dir/subdir1/file1.ext", The length of the path 21 \texttt{21} 21.
"dir/subdir2/subsubdir2/file2.ext" \texttt{"dir/subdir2/subsubdir2/file2.ext"} "dir/subdir2/subsubdir2/file2.ext", The length of the path 32 \texttt{32} 32.
return 32 \texttt{32} 32, Because this is the longest path .
Example 3:
Input : input = "a" \texttt{input = "a"} input = "a"
Output : 0 \texttt{0} 0
explain : No files exist .
Example 4:
Input : input = "file1.txt \ nfile2.txt \ nlongfile.txt" \texttt{input = "file1.txt}\backslash\texttt{nfile2.txt}\backslash\texttt{nlongfile.txt"} input = "file1.txt\nfile2.txt\nlongfile.txt"
Output : 12 \texttt{12} 12
explain : There is... In the root directory 3 \texttt{3} 3 File .
Because the absolute path of anything in the root directory is the name itself , So the answer is "longfile.txt" \texttt{"longfile.txt"} "longfile.txt", The path length is 12 \texttt{12} 12.
Data range
- 1 ≤ input.length ≤ 10 4 \texttt{1} \le \texttt{input.length} \le \texttt{10}^\texttt{4} 1≤input.length≤104
- input \texttt{input} input May contain lowercase or uppercase English letters 、 A newline ‘ \ n’ \texttt{`}\backslash\texttt{n'} ‘\n’、 tabs ‘ \ t’ \texttt{`}\backslash\texttt{t'} ‘\t’、 spot ‘.’ \texttt{`.'} ‘.’、 Space ‘ ’ \texttt{` '} ‘ ’ And number
solution
Ideas and algorithms
Because each file and folder in the file system is separated by a newline , Therefore, the given input is separated into a string array according to the newline character , Each element in the string array is a file and folder , Just process each element in the string array .
For each file or folder , The number of tabs contained at the left end of the string is the level of the file or folder , The first 0 0 0 The layer is the file or folder under the root directory . If a file or folder is not in the root directory , Then there must be a superior folder , The number of levels of the parent folder is the number of levels of the current file or folder minus 1 1 1, And the position of the parent folder in the string array must be before the current file or folder .
For file systems with hierarchical relationships , Stack can be used to maintain the relative path length of each layer , At the same time, calculate the absolute path length of each layer . Because the stack stores the relative path length of each layer , Therefore, the location of each element in the stack reflects the level of the file or folder represented by this element , When there is i i i Element time , The element at the top of the stack represents the i − 1 i - 1 i−1 The relative path length of the file or folder of the layer .
Traverse each element in the string array in turn , Maintain the elements in the stack and the absolute path length during traversal , Initially, the stack is empty , The absolute path length is 0 0 0. The specific methods are as follows :
For the current element in the string array , Calculate the number of tabs contained at the left end of the string , Get the level of the file or folder and the relative path length of the file or folder ;
If the number of elements in the stack is greater than the current level , The top of the stack element is removed from the stack , Until the number of elements in the stack is equal to the current level , At the same time, the stack elements are subtracted from the absolute path length ;
Stack the current relative path length , At the same time, add the current relative path length to the absolute path length ;
If the current element is a file , That is, the string contains a dot , Then the longest absolute path length of the file is updated according to the current absolute path length .
When calculating the relative path length of a file or folder , One detail to note is , Except for the files and folders under the root directory , The relative path of each of the remaining files and folders contains a ‘/’ \text{`/'} ‘/’, Therefore, when calculating the relative path length of files and folders , The relative path length of the file or folder under the root directory is the length of the string , The relative path length of a file or folder not under the root directory is the length of the string minus the number of tabs plus 1 1 1( Add 1 1 1 Because it contains a ‘/’ \text{`/'} ‘/’).
After the traversal , You can get the longest absolute path length of the file .
Code
class Solution {
public int lengthLongestPath(String input) {
String[] array = input.split("\n");
int maxPathLength = 0;
int pathLength = 0;
Deque<Integer> stack = new ArrayDeque<Integer>();
int arrayLength = array.length;
for (int i = 0; i < arrayLength; i++) {
String str = array[i];
int level = 0;
while (str.charAt(level) == '\t') {
level++;
}
int length = str.length() - level + (level == 0 ? 0 : 1);
while (stack.size() > level) {
pathLength -= stack.pop();
}
stack.push(length);
pathLength += length;
if (str.indexOf('.') >= 0) {
maxPathLength = Math.max(maxPathLength, pathLength);
}
}
return maxPathLength;
}
}
Complexity analysis
Time complexity : O ( n ) O(n) O(n), among n n n Is string input \textit{input} input The length of . You need to traverse the string array once , The operation time of each character in the original input is O ( 1 ) O(1) O(1).
Spatial complexity : O ( n ) O(n) O(n), among n n n Is string input \textit{input} input The length of . Space complexity mainly depends on string array and stack , The space complexity is O ( n ) O(n) O(n).
边栏推荐
猜你喜欢

Acwing 379. hide and seek problem solution (minimum path non repeating point coverage)

Prometheus + process exporter + grafana monitor the resource usage of the process

1. Mx6ul core module serial WiFi test (VIII)

1. Mx6ul core module uses serial EMMC read / write test (IV)

1. Mx6ul core module use serial RTC test (XII)

租户问题。

MySQL(4)

From a data incremental processing problem, we can see the consciousness gap of technicians

TCP三次握手四次挥手

How to choose cloud note tool? What can I do with cloud notes?
随机推荐
Li Kou daily question - day 39 -67. Binary sum
Wechat applet decryption and unpacking to obtain source code tutorial
Design and driver transplantation of matrix keyboard circuit of Ti am335x industrial control module
Adruino 基础实验学习(一)
Audio and video technology development weekly | 254
From a data incremental processing problem, we can see the consciousness gap of technicians
IDEA运行web项目出现乱码问题有效解决(附详细步骤)
商业智能BI全解析,探寻BI本质与发展趋势
Data warehouse: Practice of hierarchical structure of data warehouse in banking industry
How to choose cloud note tool? What can I do with cloud notes?
Activiti workflow gateway
U++ common type conversion and common forms and proxies of lambda
(Dynamic Programming Series) sword finger offer 48. the longest substring without repeated characters
(CVPR 2019) GSPN: Generative Shape Proposal Network for 3D Instance Segmentation in Point Cloud
scipy.sparse.csr_ matrix
17. Reverse the linked list
主键B+ Tree,二级索引B+ Tree及对应的查询过程分析
[C language brush leetcode] 814. Binary tree pruning (m)
Master-slave replication in MySQL
[C]详解语言文件操作