LeetCode //C - 388. Longest Absolute File Path

388. Longest Absolute File Path

Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture:
在这里插入图片描述
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.

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

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.
 

Example 1:

在这里插入图片描述

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:

在这里插入图片描述

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”.

Constraints:
  • 1 < = i n p u t . l e n g t h < = 1 0 4 1 <= input.length <= 10^4 1<=input.length<=104
  • input may contain lowercase or uppercase English letters, a new line character ‘\n’, a tab character ‘\t’, a dot ‘.’, a space ’ ', and digits.
  • All file and directory names have positive length.

From: LeetCode
Link: 388. Longest Absolute File Path


Solution:

Ideas:

1. Input Parsing: The input string is parsed line by line. Tabs (\t) indicate the depth (or level) of directories and files. For example, \t\t means the file is inside a subdirectory which itself is inside another subdirectory.

2. Tracking Length: An array levelLengths[] is used to store the cumulative length of the directory path up to a certain level. When a new directory or file is encountered, its path length is calculated by adding its length to the length of its parent directory.

3. File Detection: If the current string contains a dot (.), it’s identified as a file. The maximum path length is updated when a file is found.

4. Edge Cases: If no files are found, the function returns 0, as no path is valid.

Code:
int lengthLongestPath(char* input) {
    int maxLength = 0;
    int currentLength = 0;
    int levelLengths[100] = {0};  // Store the length of each level (dir, subdir, etc.)
    int level = 0;
    
    for (int i = 0; i < strlen(input); i++) {
        int start = i;
        // Find the current level
        level = 0;
        while (i < strlen(input) && input[i] == '\t') {
            level++;
            i++;
        }
        int isFile = 0;
        // Calculate the length of the current file or directory
        while (i < strlen(input) && input[i] != '\n') {
            if (input[i] == '.') isFile = 1;  // If there's a '.', it's a file
            i++;
        }
        int nameLength = i - start - level;  // Length of file/dir excluding tabs
        
        if (level > 0) {
            currentLength = levelLengths[level - 1] + 1 + nameLength;  // Parent dir + '/' + current dir/file
        } else {
            currentLength = nameLength;
        }
        
        // Update the length of the current level
        levelLengths[level] = currentLength;
        
        if (isFile) {
            if (currentLength > maxLength) {
                maxLength = currentLength;
            }
        }
    }

    return maxLength;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值