Suppose we abstract our file system by a string in the following manner:

The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents:

dir
subdir1
subdir2
file.ext
The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext. The string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" represents: dir
subdir1
file1.ext
subsubdir1
subdir2
subsubdir2
file2.ext
The directory dir contains two sub-directories subdir1 and subdir2. subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1. subdir2 contains a second-level sub-directory subsubdir2 containing a file file2.ext. We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is "dir/subdir2/subsubdir2/file2.ext", and its length is 32 (not including the double quotes). Given a string representing the file system in the above format, return the length of the longest absolute path to file in the abstracted file system. If there is no file in the system, return 0. Note:
The name of a file contains at least a . and an extension.
The name of a directory or sub-directory will not contain a ..
Time complexity required: O(n) where n is the size of the input string. Notice that a/aa/aaa/file1.txt is not the longest file path, if there is another path aaaaaaaaaaaaaaaaaaaaa/sth.png.

Time Complexity: O(N)

The depth of the directory/file is calculated by counting how many "\t"s are there.
The time complexity is O(n) because each substring in the input string only goes into the stack once, and pops out from the stack once.

 public class Solution {
public int lengthLongestPath(String input) {
if (input==null || input.length()==0) return 0;
int maxLen = 0;
int curLen = 0;
Stack<Integer> stack = new Stack<Integer>();
String[] strs = input.split("\n");
for (int i=0; i<strs.length; i++) {
int level = countLev(strs[i]); while (stack.size() > level) {
curLen -= stack.pop();
} // +1 here because a "/" needs to be counted at the end of each diretory
int strLen = strs[i].replaceAll("\t", "").length() + 1;
curLen += strLen; // if s contains ".", we have found a file
if (strs[i].contains(".")) {
maxLen = Math.max(maxLen, curLen-1); //"/" is not needed at the end of the file
}
stack.push(strLen);
}
return maxLen;
} public int countLev(String str) {
String strReduced = str.replaceAll("\t", "");
return str.length() - strReduced.length();
}
}

Faster solution: avoid string operation

 public class Solution {
public int lengthLongestPath(String input) {
if (input==null || input.length()==0) return 0;
int maxLen = 0;
int curLen = 0;
Stack<Integer> stack = new Stack<Integer>();
String[] strs = input.split("\n");
for (int i=0; i<strs.length; i++) {
int level = countLev(strs[i]); while (stack.size() > level) {
curLen -= stack.pop();
} int strLen = strs[i].length() - level + 1;
curLen += strLen; if (strs[i].contains(".")) {
maxLen = Math.max(maxLen, curLen-1);
}
stack.push(strLen);
}
return maxLen;
} public int countLev(String str) {
int level = 0;
for (int i=0; i<str.length(); i++) {
if (str.charAt(i) == '\t') level++;
else break; }
return level;
}
}

Leetcode: Longest Absolute File Path的更多相关文章

  1. [LeetCode] Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  2. [LeetCode] 388. Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  3. 【LeetCode】388. Longest Absolute File Path 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 日期 题目地址:https://leetcode. ...

  4. Leetcode算法比赛----Longest Absolute File Path

    问题描述 Suppose we abstract our file system by a string in the following manner: The string "dir\n ...

  5. Longest Absolute File Path -- LeetCode

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  6. 【leetcode】388. Longest Absolute File Path

    题目如下: Suppose we abstract our file system by a string in the following manner: The string "dir\ ...

  7. [Swift]LeetCode388. 文件的最长绝对路径 | Longest Absolute File Path

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  8. Longest Absolute File Path

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  9. 388. Longest Absolute File Path

    就是看哪个文件的绝对路径最长,不是看最深,是看最长,跟文件夹名,文件名都有关. \n表示一波,可能存在一个文件,可能只有文件夹,但是我们需要检测. 之后的\t表示层数. 思路是如果当前层数多余已经有的 ...

随机推荐

  1. 浅谈XML

    什么是 XML? · XML 指可扩展标记语言(EXtensible Markup Language) · XML 是一种标记语言,很类似 HTML · XML 的设计宗旨是传输数据,而非显示数据 · ...

  2. jquery()的三种$()

    jQuery中的$以及选择器总结 $号是jQuery”类”的一个别称,$()构造了一个jQuery对象.所以,”$()”可以看作jQuery的”构造函数”(个人观点). 一.$符号 1.$()可以是$ ...

  3. 理解OAuth 2.0[摘]

    原文地址:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到 ...

  4. mysqli_query($link,'SET group_concat_max_len=8192');

    mysqli_query($link,'SET group_concat_max_len=8192'); $sql = 'SELECT GROUP_CONCAT(w) FROM ---'; mysql ...

  5. = splice

    <script> function wf(w){ console.log(w); } var wa = [3,66,7]; var wb = wa; wa.splice(1,1); wf( ...

  6. ajax+jsp自动刷新

    通过 AJAX,JavaScript 可使用 JavaScript 的 XMLHttpRequest 对象来直接与服务器进行通信.通过这个对象, JavaScript 可在不重载页面的情况与 Web ...

  7. svn截图

        一.合并一个范围的版本 此类型应用最为广泛,主要是把分支中的修改合并到主干上来.在主干上点击右键选择合并,然后选择合并类型:合并一个范围的版本.合并的源URL填写的是要合并的分支的URL,待合 ...

  8. 费用性支出预分摊form方式和web方式区别

    预分摊 1 form方式 费用性支出先通过生产资产行请求 生成一个资产行,如果该资产行分摊到两个资产上则分割成两个资产行,既所谓的针对资产行进行分摊. 2 web方式 费用性支出直接分摊到资产上形成资 ...

  9. Apache Camel

    Apache Camel 1 import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; i ...

  10. 怎么样用opencv将彩色图片转化成像素值只有0和255的灰度图?

      分类: OpenCV [Q1]怎么样用opencv将彩色图片转化成像素值只有0和255的灰度图? 进行灰度化,IplImage* pImg = cvLoadImage( "C:\\1.b ...