388. Longest Absolute File Path
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the length of the longest absolute path to a file within a file system represented as a single string. The structure is hierarchical, with directories and files. The depth of each entry is indicated by the number of tab characters (\t) preceding its name, and entries are separated by newline characters (\n). An absolute path is the concatenation of directory names from the root to the file, joined by /. For instance, a file file.ext inside subdir which is in dir has the path dir/subdir/file.ext.
Solution
Longest Absolute File Path in Python
class Solution:
def lengthLongestPath(self, input: str) -> int:
max_len = 0
# Map from depth to the length of the path up to that depth.
# We use a dummy entry for depth -1 to simplify logic for root items.
path_lengths = {-1: 0}
# Process the file system representation line by line.
for line in input.split('\n'):
# Determine the depth of the current file/directory.
# The depth is the number of leading '\t' characters.
depth = line.rfind('\t') + 1
# Extract the name length of the file/directory.
name_len = len(line) - depth
# Calculate the length of the absolute path to the current item.
# It's the parent's path length + 1 (for '/') + current item's name length.
current_len = path_lengths[depth - 1] + name_len + (1 if depth > 0 else 0)
if '.' in line:
# If it's a file, update the maximum length found so far.
max_len = max(max_len, current_len)
else:
# If it's a directory, store its path length for its children to use.
path_lengths[depth] = current_len
return max_len
Longest Absolute File Path in Java
class Solution {
public int lengthLongestPath(String input) {
int maxLen = 0;
// Map from depth to the length of the path up to that depth.
// We use a dummy entry for depth -1 to simplify logic for root items.
Map<Integer, Integer> pathLengths = new HashMap<>();
pathLengths.put(-1, 0);
String[] lines = input.split("\n");
// Process the file system representation line by line.
for (String line : lines) {
// Determine the depth of the current file/directory.
// The depth is the number of leading '\t' characters.
int depth = line.lastIndexOf('\t') + 1;
// Extract the name of the file/directory.
String name = line.substring(depth);
// Calculate the length of the absolute path to the current item.
// It's the parent's path length + 1 (for '/') + current item's name length.
int currentLen = pathLengths.get(depth - 1) + name.length() + (depth > 0 ? 1 : 0);
if (name.contains(".")) {
// If it's a file, update the maximum length found so far.
maxLen = Math.max(maxLen, currentLen);
} else {
// If it's a directory, store its path length for its children to use.
pathLengths.put(depth, currentLen);
}
}
return maxLen;
}
}
Longest Absolute File Path in C++
class Solution {
public:
int lengthLongestPath(string input) {
int max_len = 0;
// Map from depth to the length of the path up to that depth.
// We use a dummy entry for depth -1 to simplify logic for root items.
std::unordered_map<int, int> path_lengths;
path_lengths[-1] = 0;
std::stringstream ss(input);
std::string line;
// Process the file system representation line by line.
while (std::getline(ss, line, '\n')) {
// Determine the depth of the current file/directory.
// The depth is the number of leading '\t' characters.
// find_last_of returns the index of the last tab. Depth is that index + 1.
size_t last_tab_pos = line.find_last_of('\t');
int depth = (last_tab_pos == std::string::npos) ? 0 : last_tab_pos + 1;
// Extract the name length of the file/directory.
size_t name_len = line.length() - depth;
// Calculate the length of the absolute path to the current item.
// It's the parent's path length + 1 (for '/') + current item's name length.
int current_len = path_lengths[depth - 1] + name_len + (depth > 0 ? 1 : 0);
if (line.find('.') != std::string::npos) {
// If it's a file, update the maximum length found so far.
max_len = std::max(max_len, current_len);
} else {
// If it's a directory, store its path length for its children to use.
path_lengths[depth] = current_len;
}
}
return max_len;
}
};
Longest Absolute File Path in TypeScript
function lengthLongestPath(input: string): number {
let maxLen = 0;
// Map from depth to the length of the path up to that depth.
// We use a dummy entry for depth -1 to simplify logic for root items.
const pathLengths: Map<number, number> = new Map();
pathLengths.set(-1, 0);
const lines = input.split('\n');
// Process the file system representation line by line.
for (const line of lines) {
// Determine the depth of the current file/directory.
// The depth is the number of leading '\t' characters.
const depth = line.lastIndexOf('\t') + 1;
// Extract the name of the file/directory.
const name = line.substring(depth);
// Calculate the length of the absolute path to the current item.
// It's the parent's path length + 1 (for '/') + current item's name length.
const parentLen = pathLengths.get(depth - 1) || 0;
const currentLen = parentLen + name.length + (depth > 0 ? 1 : 0);
if (name.includes('.')) {
// If it's a file, update the maximum length found so far.
maxLen = Math.max(maxLen, currentLen);
} else {
// If it's a directory, store its path length for its children to use.
pathLengths.set(depth, currentLen);
}
}
return maxLen;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.