Problem Statement (Asked By Google)
A file system can be represented using a string, where:
\n
(new lines).\t
).For example, the string:
"dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
Represents:
dir
subdir1
subdir2
file.ext
The root directory dir
contains:
subdir1
(empty)subdir2
containing the file file.ext
Another example:
"dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
Represents:
dir
subdir1
file1.ext
subsubdir1
subdir2
subsubdir2
file2.ext
dir
contains: subdir1
with: file1.ext
subsubdir1
(empty)subdir2
with: subsubdir2
containing file file2.ext
Find the length of the longest absolute path to a file within the abstracted file system. The length is measured in characters.
For the second example, the longest path is:
"dir/subdir2/subsubdir2/file2.ext"
.
) for the extension.Problem link: https://leetcode.com/problems/longest-absolute-file-path/
Disclaimer: Try solving the problem on your own first! Use this solution only as a reference to learn and improve.
We can break the problem into two key steps:
Given a string representation of the file system, we aim to parse it into a structured form, such as a tree or dictionary.
For example, given:
dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext
We want a structure like this:
dir ├── subdir1 │ ├── file1.ext │ └── subsubdir1 └── subdir2 └── subsubdir2 └── file2.ext
To achieve this:
\n
), so each line represents a file or directory.\t
) to determine the directory hierarchy..
), record its absolute path length.This approach ensures we only traverse the string once, achieving O(n) time complexity, where n is the length of the input string.
import java.util.Stack; public class LongestAbsolutePath { public static int longestAbsolutePath(String input) { // Stack stores the length of each directory level Stack<Integer> stack = new Stack<>(); stack.push(0); // Base length for the root directory int maxLength = 0; // Split the input into lines String[] lines = input.split("\n"); for (String line : lines) { // Count number of tabs (depth) int level = line.lastIndexOf("\t") + 1; // Remove tabs to get the actual name String name = line.substring(level); // Adjust the stack to the current directory level while (stack.size() > level + 1) { stack.pop(); } // Get the length of the current path int length = stack.peek() + name.length() + 1; // +1 for '/' if (name.contains(".")) { // It's a file, check if it's the longest path maxLength = Math.max(maxLength, length - 1); // -1 to remove trailing '/' } else { // It's a directory, store length for deeper levels stack.push(length); } } return maxLength; } public static void main(String[] args) { String input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"; System.out.println(longestAbsolutePath(input)); // Output: 32 } }
String[] lines = input.split("\n")
: Splits the string into directories and files.int level = line.lastIndexOf("\t") + 1
: Counts the number of tabs to determine the depth.String name = line.substring(level)
: Gets the actual directory or file name./
).For the input:
dir subdir1 file1.ext subsubdir1 subdir2 subsubdir2 file2.ext
Stack Operations:
dir
: push length.subdir1
: push length.file1.ext
: compute full path length.subdir2
: pop up to dir
, push length.file2.ext
: compute full path length.The longest path is:
dir/subdir2/subsubdir2/file2.ext
Length: 32 characters.
This solution uses Stack
and String
operations to compute the longest absolute path efficiently.
Did this solution help you understand the concept better? Let me know your thoughts in the comments below! Don’t forget to follow us on Instagram @coderz.py for more DSA tips and solutions to ace your coding interviews.
You are given two singly linked lists that intersect at some node. Your task is…
A builder plans to construct N houses in a row, where each house can be…
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…