{"id":10921,"date":"2019-02-11T03:30:40","date_gmt":"2019-02-11T03:30:40","guid":{"rendered":"http:\/\/www.softwareeverydayblog.com\/?p=10921"},"modified":"2019-02-11T03:30:40","modified_gmt":"2019-02-11T03:30:40","slug":"simplify-path","status":"publish","type":"post","link":"https:\/\/www.softwareeverydayblog.com\/?p=10921","title":{"rendered":"Simplify Path"},"content":{"rendered":"<p>[<a href=\"https:\/\/leetcode.com\/problems\/simplify-path\/description\/\" rel=\"noopener\" target=\"_blank\">Problem Link<\/a>]<\/p>\n<ul>\n<li>Tokenize the string with &#8216;\/&#8217; as a delimiter.<\/li>\n<li>Create a graph where nodes have pointers to parent nodes.<\/li>\n<li>Keep moving up(&#038; down) the graph depending on the directory.<\/li>\n<li>If you see a directory, create (or move) to the node (depending on if it exists or not)!<\/li>\n<li>Make sure you don&#8217;t move above the root node because &#8220;Going one level up from the root directory is a no-op&#8221;.<\/li>\n<li>When you&#8217;re done, traverse up to the root node (while creating the path).<\/li>\n<li>If the path is empty, return &#8220;\/&#8221;<\/li>\n<\/ul>\n<pre lang=\"javascript\">\r\n\/**\r\n * @param {string} path\r\n * @return {string}\r\n *\/\r\n\r\nfunction node(val, parent) {\r\n  this.val = val;\r\n  this.parent = parent;\r\n  this.children = [];\r\n    \r\n  this.findChild = function(val) {\r\n    var result = this.children.filter((child) => child.val === val);\r\n    return result && result.length === 1 && result[0];\r\n  }\r\n}\r\n\r\nvar simplifyPath = function(path) {\r\n  var root = new node('', null);\r\n  path.split(\"\/\").forEach((dir) => {\r\n    if ( dir ) {\r\n      if ( dir === '..' ) {\r\n        if (root.parent) {\r\n          root = root.parent;\r\n        }\r\n      } else if ( dir !== '.' ) {\r\n        var child = root.findChild(dir);\r\n        if (!child) {\r\n          child = new node('\/' + dir, root);\r\n          root.children.push(child);\r\n        }\r\n        root = child;\r\n      }\r\n    }\r\n  })\r\n    \r\n  var ret = '';\r\n  var curr = root;\r\n  while (curr) {\r\n    ret = curr.val + ret;\r\n    curr = curr.parent;\r\n  }\r\n  \r\n  return ret ? ret : '\/';\r\n};\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>[Problem Link] Tokenize the string with &#8216;\/&#8217; as a delimiter. Create a graph where nodes have pointers to parent nodes. Keep moving up(&#038; down) the graph depending on the directory. If you see a directory, create (or move) to the node (depending on if it exists or not)! Make sure you don&#8217;t move above the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10921","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/10921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10921"}],"version-history":[{"count":2,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/10921\/revisions"}],"predecessor-version":[{"id":10923,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/10921\/revisions\/10923"}],"wp:attachment":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}