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. Delphi指针及其它(转)

    一.指针:指向一个内存地址的变量或参数. 二.定义指针的方式如下: P: Pointer; //定义了可以指向任何类型的指针,Pointer 为无类型指针: Q, R: ^TType; //定义了指向 ...

  2. Tab指示符——Indicator

    先说说我们的思路吧. 其实思路也很简单,就是在咱们的导航下面画一个小矩形,不断的改变这个矩形距离左边的位置. 思路就这么简单,有了思路,接下来就是实现了,看代码: public class Indic ...

  3. c/c++编译时,指定程序运行时查找的动态链接库路径

    http://blog.csdn.net/tsxw24/article/details/10220735 c/c++编译时,指定程序运行时查找的动态链接库路径 分类: c/c++ linux 2013 ...

  4. Bluetooth ATT介绍

    目录 1 介绍 2 详细内容 2.1 Attribute Type 2.2 Attribute Handle 2.3 Attribute Handle Grouping 2.4 Attribute V ...

  5. Bluetooth HCI介绍

    目录 1. HCI功能 2. HCI Packet 1. HCI Command 2. HCI Event 3. HCI Data 3. HCI传输层 HCI, 主机控制接口(Host Control ...

  6. Qt 之 自定义提示信息框—迅雷风格(模拟QDialog类的exec()方法) good

    http://blog.csdn.net/goforwardtostep/article/details/53614830

  7. BAT(批处理)获得参数

    原文转自:http://blog.csdn.net/luhouxiang/article/details/31733049 获取参数有2种, 一种为从命令行输入参数,使用两个%中间包含数字表示,数字从 ...

  8. Android 开发-Intent传递普通字符串

    假设A传递id到B中 ActivityA: Intent intent=new Intent();    intent.setClass(ActivityA.this,ActivityB.class) ...

  9. LightOj 1090 - Trailing Zeroes (II)---求末尾0的个数

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1090 题意:给你四个数 n, r, p, q 求C(n, r) * p^q的结果中末尾 ...

  10. JS判断浏览器是否安装flash插件

    1.直接判断是否有flash插件 var myFlash = (function(){ if(typeof window.ActiveXObject != "undefined") ...