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表示层数. 思路是如果当前层数多余已经有的 ...
随机推荐
- Cacti安装详细步骤
原文链接: https://www.centos.bz/2012/01/cacti-install-tutorials/ Cacti-监控MySQL: http://www.cszhi.com/201 ...
- Object-relational mapping
https://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM, O/RM, and O/ ...
- Natural Language Toolkit
http://www.nltk.org/ >>> import nltk >>> nltk.download()
- __LINE__ check_arr_empty($arr)
<?php $arr = array('','',''); foreach($arr as $w) { // w(empty($w)); } w(empty($arr)); w(check_ar ...
- Spring IoC反转控制的快速入门
* 下载Spring最新开发包 * 复制Spring开发jar包到工程 * 理解IoC反转控制和DI依赖注入 * 编写Spring核心配置文件 * 在程序中读取Spring配置文件,通过Spring框 ...
- url匹配和match()方法
下面的全局匹配可以找到字符串中的所有数字: "1 plus 2 equals 3".match(/\d+/g) // 返回 ["1", "2" ...
- C# 64位系统中类型所占空间大小
Boolean 8Byte DateTime 8Byte Decimal 16Byte String 引用地址空间8Bypte Int 4Bypte 类所占空间大小 (byte):各个filed ...
- node.js的exprots工厂模式
工厂类: /** * Created by zzq on 2015/5/15. */ module.exports = function(){ this.getProduct = function() ...
- js中将字符串转换成json的三种方式
1,eval方式解析,恐怕这是最早的解析方式了.如下: function strToJson(str){ var json = eval('(' + str + ')'); return json; ...
- interview review
缘起: 因为最近要找工作,自己总结了一下面试的注意事项. 1自我介绍方法 1.基本情况:姓名.年龄.学历.家庭与理想. 简单明了,不要啰嗦. 2.学习能力:专业知识.勤奋好学. 用事实说明学习能力不错 ...