Leetcode:longest Absolute File Path

Source: Internet
Author: User

Suppose weAbstractOur 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.extthe 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.extthe dir Ectory dir contains, 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 is interested in finding the longest (number of characters) Absol Ute 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 + (not including theDoublequotes). Given A string representing the file system in the above format,returnThe length of the longest absolute path to file in the abstracted file system. If There is no file in the system,return0. Note:the name of a file contains at least a. and an extension. The name of a directory or sub-directory would 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,ifThere 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 with the input string only goes to the stack once, and pops out from th e stack once.

1  Public classSolution {2      Public intLengthlongestpath (String input) {3         if(input==NULL|| Input.length () ==0)return0;4         intMaxLen = 0;5         intCurlen = 0;6stack<integer> stack =NewStack<integer>();7string[] STRs = input.split ("\ n");8          for(inti=0; i<strs.length; i++) {9             intLevel =Countlev (Strs[i]);Ten              One              while(Stack.size () >Level ) { ACurlen-=Stack.pop (); -             } -              the             //+1 here because a "/" needs to being counted at the end of each diretory -             intStrLen = 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 A             } at Stack.push (strLen); -         } -         returnMaxLen; -     } -      -      Public intCountlev (String str) { inString strreduced = Str.replaceall ("\ T", "" "); -         returnStr.length ()-strreduced.length (); to     } +}

Leetcode:longest Absolute File Path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.