Leetcode: Longest Absolute File Path
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的更多相关文章
- [LeetCode] Longest Absolute File Path 最长的绝对文件路径
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- [LeetCode] 388. Longest Absolute File Path 最长的绝对文件路径
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- 【LeetCode】388. Longest Absolute File Path 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 日期 题目地址:https://leetcode. ...
- Leetcode算法比赛----Longest Absolute File Path
问题描述 Suppose we abstract our file system by a string in the following manner: The string "dir\n ...
- Longest Absolute File Path -- LeetCode
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- 【leetcode】388. Longest Absolute File Path
题目如下: Suppose we abstract our file system by a string in the following manner: The string "dir\ ...
- [Swift]LeetCode388. 文件的最长绝对路径 | Longest Absolute File Path
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- Longest Absolute File Path
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- 388. Longest Absolute File Path
就是看哪个文件的绝对路径最长,不是看最深,是看最长,跟文件夹名,文件名都有关. \n表示一波,可能存在一个文件,可能只有文件夹,但是我们需要检测. 之后的\t表示层数. 思路是如果当前层数多余已经有的 ...
随机推荐
- 图片lightbox2
1. 官网下载 http://lokeshdhakar.com/projects/lightbox2/ 2.引入 css jquery js 3. HTML格式 <a href=" ...
- Class Abstraction -- Object Interfaces
<?php /* PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be in ...
- Java中的可变参数以及foreach语句
Java中的可变参数的定义格式如下: 返回值类型 方法名称(类型 ... 参数名称){} foreach语句的格式如下: for ( 数据类型 变量名称 :数据名称){ ... } public ...
- Running Kafka At Scale
https://engineering.linkedin.com/kafka/running-kafka-scale If data is the lifeblood of high technolo ...
- extjs4 树列表 添加子节点 刷新所有父节点数据
itemclick:function(view, record, item,index){console.log(record.parentNode) for(pNode = record.paren ...
- 添加Ubuntu的库文件路径
添加Ubuntu的库文件路径 http://blog.csdn.net/r91987/article/details/6879062 关于ubuntu添加共享库路径: 1. 将绝对路径写入 /etc/ ...
- PySe-001-基础环境配置(MacOX)
Python 是一种面向对象.解释型计算机程序设计语言,其源代码同样遵循 GPL(GNU General Public License)协议.Python语法简洁而清晰,具有丰富和强大的类库.由于Py ...
- JavaScript实现dropdownlist选定值后将选定值的key与value填入两个textbox中
<script language="javascript" type="text/javascript"> var txtText0 = " ...
- Windows下使用Git和GitHub.com
1.首先介绍一下什么是Git和GitHub Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理.在推出后,Git在其它项目中也取得了很大 ...
- Java: arr==null vs arr.length==0
当 arr 是一个array时,写Java开始的corner case常常会写类似下面的语句: if(arr == null || arr.length == 0){ return 0; } 其实这是 ...