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. Android TextView 显示不全的自动补齐方式

    TextView在Android开发中用到的地方应该是很多的.很多时候,TextView会有一行显示不全被截取或者会换行.之前我的解决办法比较笨拙,定死TextView的一行字数长度,最后一个以省略号 ...

  2. setattribute兼容

    var spanElement = document.getElementById("mySpan"); spanElement.style.cssText = "fon ...

  3. 蓝牙4.0的LM层说明

    1.概念 The Link Manager Protocol (LMP) is used to control and negotiate all aspects of the operation o ...

  4. pomelo架构概览

    pomelo之所以简单易用.功能全面,并且具有高可扩展性.可伸缩性等特点,这与它的技术选型和方案设计是密不可分的.在研究大量游戏引擎设计思路基础上,结合以往游戏开发的经验,确定了pomelo框架的设计 ...

  5. 报javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception异常 的解决方案

    后台错误信息如下: javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception org.a ...

  6. CSS优先级别计算

    a.b.c.d,可以以这四种等级为依据确定CSS选择器的优先级: a-----style 行内样式 个数  +1000 b-----id 个数+100 c-----类 个数+10 d-----类型个数 ...

  7. NAT123 解决80端口被封的问题

    使用的服务器不知什么原因80端口无法使用了,好像是被封了,用的移动的固定IP,移动线路一直是不稳定 关键是移动的回答竟然是找不到哪里封的 是不是被屏蔽了,无奈使用了NAT123做处理.试了下还是管用. ...

  8. JMeter学习-008-JMeter 后置处理器实例之 - 正则表达式提取器(一)概述及简单实例

    上文我们讲述了如何对 HTTP请求 的响应数据进行断言,以判断响应是否符合我们的预期,敬请参阅:JMeter学习-007-JMeter 断言实例之一 - 响应断言 那么我们如何获取 HTTP请求 响应 ...

  9. iOS项目的目录结构和开发流程(Cocoa China)

    目录结构 AppDelegate Models Macro General Helpers Vendors Sections Resources   一个合理的目录结构首先应该是清晰的,让人一眼看上去 ...

  10. shell 脚本文件Windows传到Linux后编码问题

    shell 脚本文件Windows传到Linux后编码问题 下面这个标红的位置出现,是由于脚本从Windows机器上直接传到linux文件格式不对导致的. cat -v a.sh help^M exi ...