Simplify Path
LeetCode 71 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Stack of directory names.
நான் Simplify Path (LeetCode 71) ஐ இப்படித் தீர்த்தேன் — Stack Simulation / String Processing பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
Split the path on '/' and process the pieces with a stack: '..' pops a directory, '.' and empty pieces do nothing, anything else is a directory name to push.
Approach
- 1
Split by '/'.
- 2
Skip '' and '.'.
- 3
'..' → pop if possible.
- 4
Push other names; join with '/' and prefix '/'.
நான் எப்படித் தீர்த்தேன்
Stack of componentsPythonO(n) time
class Solution:
def simplifyPath(self, path: str) -> str:
#/.../a/../b/c/../d/./
#/directory_name/ [a/..] - >/directory_name/
#/directory_name/b/c/.. ->/directory_name/b/
#/directory_name/b/d/. -> /.../b/d
#intuition is -
#/d/./ -> /d
#if it's single dot (.) we just ignoring . and taking the previous path /d
#/user/Documents/.. ->/user
#if it's a double dot then we are removing Documents
#approach
#have a stack
#split string by /
#if cur string is . no need to do anything
#if cur string is .. pop from stack
#rest - add to stack
#finally join by / and return
stack =[]
components = path.split("/")
for component in components:
if component=="." or component=="":
continue
elif component=="..":
if len(stack)>0:
stack.pop()
else:
stack.append(component)
return "/"+"/".join(stack)Simplify Path — அடிக்கடி கேட்கப்படும் கேள்விகள்
Simplify Path பின்னாலுள்ள intuition என்ன?
Split the path on '/' and process the pieces with a stack: '..' pops a directory, '.' and empty pieces do nothing, anything else is a directory name to push.
Simplify Path (LeetCode 71) ஐ எப்படித் தீர்ப்பது?
1. Split by '/'. 2. Skip '' and '.'. 3. '..' → pop if possible. 4. Push other names; join with '/' and prefix '/'.
Simplify Path இன் time மற்றும் space complexity என்ன?
Time: O(n). Space: O(n). (Accepted approach: Stack of components.)
Simplify Path இல் பொதுவான தவறுகள் என்ன?
'...' is a valid directory name, not a parent reference.