-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC_Absolute_File_Path.py
31 lines (30 loc) · 1.32 KB
/
LC_Absolute_File_Path.py
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
26
27
28
29
30
31
# Longest Absolute File Path
# https://leetcode.com/problems/longest-absolute-file-path/
class Solution:
def lengthLongestPath(self, input):
"""
:type input: str
:rtype: int
"""
# Split the input based on new line
lines = input.split('\n')
longest = 0;
# Create an array of None. This will be used to store the lenght for each level.
level_value = [None] * len(lines)
for line in lines:
# find the position of the last tab space.
level = line.rfind('\t') + 1
# find the length of the text
length = len(line)- level + 1
if (level > 0):
# add the lenght till the previous level to the lenght nd store it back in the level
level_value[level] = level_value[level-1] + length
elif (level == 0):
# store the length of the level in the array
level_value[0] = length
if ( -1 != line.find(".")):
# When we find the file (found by the dot) take the level value and do a -1 for the last / added.
# Compare it with the longest value and store it.
if (level_value[level] -1 > longest):
longest = level_value[level] -1
return longest