From 0ae5426991164b6e9f655515122dc8fc1edcedcb Mon Sep 17 00:00:00 2001 From: Aadil <77232799+aadil42@users.noreply.github.com> Date: Wed, 21 Jun 2023 20:17:50 +0530 Subject: [PATCH 1/4] Create 0071-simplify-path.js Solved simplify-path in JS. --- javascript/0071-simplify-path.js | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 javascript/0071-simplify-path.js diff --git a/javascript/0071-simplify-path.js b/javascript/0071-simplify-path.js new file mode 100644 index 000000000..bd0dcfe9a --- /dev/null +++ b/javascript/0071-simplify-path.js @@ -0,0 +1,33 @@ +/** + * Stack + * https://leetcode.com/problems/simplify-path/ + * + * Time O(n) | Space O(n) + * @param {string} path + * @return {string} + */ +var simplifyPath = function(path) { + let currunt = ''; + let myStack = []; + path = '/' + path + '/'; + for(let i = 0; i < path.length; i++) { + + console.log(myStack); + if(path[i] === '/') { + if(currunt == '..') { + if(myStack.length) { + myStack.pop(); + } + } else if(currunt !== '' && currunt !== '.') { + myStack.push(currunt); + } + currunt = ''; + } else { + currunt += path[i]; + } + } + + myStack = myStack.join('/'); + myStack = '/' + myStack; + return myStack; +}; From ffc213f059a9cd617da9b3e52f1593f9303f0eab Mon Sep 17 00:00:00 2001 From: Aadil <77232799+aadil42@users.noreply.github.com> Date: Sun, 2 Jul 2023 21:19:37 +0530 Subject: [PATCH 2/4] Update 0071-simplify-path.js using backticks --- javascript/0071-simplify-path.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javascript/0071-simplify-path.js b/javascript/0071-simplify-path.js index bd0dcfe9a..7bc9b6ca0 100644 --- a/javascript/0071-simplify-path.js +++ b/javascript/0071-simplify-path.js @@ -9,7 +9,7 @@ var simplifyPath = function(path) { let currunt = ''; let myStack = []; - path = '/' + path + '/'; + path = `/${path}/`; for(let i = 0; i < path.length; i++) { console.log(myStack); From 7bd673b93558171d58bc3ccb0b8bc09428212345 Mon Sep 17 00:00:00 2001 From: Aadil <77232799+aadil42@users.noreply.github.com> Date: Mon, 3 Jul 2023 17:24:41 +0530 Subject: [PATCH 3/4] Update 0071-simplify-path.js Removed unnecessary nested if statment --- javascript/0071-simplify-path.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/javascript/0071-simplify-path.js b/javascript/0071-simplify-path.js index 7bc9b6ca0..8a4faa3c3 100644 --- a/javascript/0071-simplify-path.js +++ b/javascript/0071-simplify-path.js @@ -12,12 +12,9 @@ var simplifyPath = function(path) { path = `/${path}/`; for(let i = 0; i < path.length; i++) { - console.log(myStack); if(path[i] === '/') { if(currunt == '..') { - if(myStack.length) { - myStack.pop(); - } + myStack.pop(); } else if(currunt !== '' && currunt !== '.') { myStack.push(currunt); } @@ -25,6 +22,7 @@ var simplifyPath = function(path) { } else { currunt += path[i]; } + } myStack = myStack.join('/'); From f65d0738013d9ad51f1f1c997644398b614c6de3 Mon Sep 17 00:00:00 2001 From: Aadil <77232799+aadil42@users.noreply.github.com> Date: Tue, 4 Jul 2023 20:04:19 +0530 Subject: [PATCH 4/4] Update 0071-simplify-path.js Modularizing the code. --- javascript/0071-simplify-path.js | 55 +++++++++++++++++--------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/javascript/0071-simplify-path.js b/javascript/0071-simplify-path.js index 8a4faa3c3..0bdaef80b 100644 --- a/javascript/0071-simplify-path.js +++ b/javascript/0071-simplify-path.js @@ -1,31 +1,36 @@ + /** * Stack - * https://leetcode.com/problems/simplify-path/ - * - * Time O(n) | Space O(n) + * Time O(N) | Space O(N) + * https://leetcode.com/problems/simplify-path * @param {string} path * @return {string} */ -var simplifyPath = function(path) { - let currunt = ''; - let myStack = []; - path = `/${path}/`; - for(let i = 0; i < path.length; i++) { - - if(path[i] === '/') { - if(currunt == '..') { - myStack.pop(); - } else if(currunt !== '' && currunt !== '.') { - myStack.push(currunt); - } - currunt = ''; - } else { - currunt += path[i]; - } - - } - - myStack = myStack.join('/'); - myStack = '/' + myStack; - return myStack; +var simplifyPath = (path, slash = '/', stack = []) => { + const paths = path.split(slash).filter(Boolean); + + for (const _path of paths) traversePath(_path, stack); + + return `${slash}${stack.join(slash)}`; +}; + +const traversePath = (path, stack) => { + if (canPush(path)) return stack.push(path); + + if (canPop(path, stack)) stack.pop(); }; + +const canPush = (path) => !( + isCurrentDirectory(path) || + isParentDirectory(path) +); + +const canPop = (path, stack) => + isParentDirectory(path) && + !isEmpty(stack); + +const isCurrentDirectory = (path) => (path === '.'); + +const isParentDirectory = (path) => (path === '..'); + +const isEmpty = ({ length }) => (0 === length);