USing Java you are given a listing of directories and files
USing Java you are given a listing of directories and files in a file system. Each directory and file has a name which is a non empty string consisting of alpha numerical characters. Additionally te name of each file contains asingle dot character; the part of the name starting with the dot is called the extension. Directory names do not contain any dots. All the names are case senstitive. Each entry is listed on a separate line. Every directory is followed by the listing of its contents indented by one space character. The contents of the root direcotry are not indented. Here is a sample listing
dir1
dir11
dir12
picture.jpeg
dir121
file1.txt
dir2
file2.gif
We have three files (picture.jpeg, file1.txt and file2.gif) an six directories. DIrectory dir12 contains two files and an empty directory. THe root directory contains two directories
The absolute path of a directory is a string containing the names of directories which have to be traversed to reach the directory separated by the slash characters. For example the absolute path to the directory dir121 is \"/dir1/dir12/dir121\" Note that there is no dirve letter such as C: and each absolute path starts with a slash. THe absolute path of a root directory is \"/\"
We are only interested in directories containing image files. We are lookinf ofr the longest absolute path leading to some directory containing an image file.
Write a function : class solution { public int solution(String S);}
that given a string S consisting of N characters which contains the listing of a file system, returns the maximum length (in characters) of an absolute path to some directory containing an image file. For example, given the sample listing shown above, should return 11, as explained above. If there are no image files in the listing, the function should return 0;
String S consists of alphanumerical characters ,spaces, dots, and end of line characters.
Expected worst case time complexity is O(N)
N is an integer within 1-3000000
Solution
Here is the simple code :
public static int printSum(String s)
{
String[] arr=s.split(\"\ \");
int sum=0, spaces=0;
for(int i=arr.length-1;i>=0;i--)
{
String line=arr[i];
if(line.contains(\".gif\") | line.contains(\".jpeg\") )
{
spaces=line.length()-line.trim().length();
}
if(spaces> line.length()-line.trim().length() )
{
sum+=line.trim().length()+1;
spaces--;
}
}
return sum;
}
And here is the another code which is another type in java you can consider in both of these according to your wish
import java.util.*;
class GoogleDirStructSearch
{
List<Integer> levelCounter = new ArrayList<Integer>();
List<Boolean> foundAtLevel = new ArrayList<Boolean>();
int getCount(String struct)
{
String structArr[] = struct.split(\"\ \");
int count = 0;
for(int i=0; i<structArr.length; i++)
{
int afterTrimLen = structArr[i].trim().length();
int currSpaceCount = structArr[i].length()-afterTrimLen;
if(structArr[i].indexOf(\".\") == -1)
{
maintainLevelCounter(afterTrimLen+1,currSpaceCount);//+1 for \'/\'
}
else if(!foundAtLevel.get(currSpaceCount-1))
{
if(structArr[i].contains(\"jpeg\") || structArr[i].contains(\"png\") || structArr[i].contains(\"gif\"))
{
count += levelCounter.get(currSpaceCount-1);
foundAtLevel.set(currSpaceCount-1,true);
}
}
}
return count;
}
void maintainLevelCounter(int count, int index)
{
int prevCount = 0;
if(index>0)
prevCount = levelCounter.get(index-1);
if(levelCounter.size()<=index)
{
levelCounter.add(prevCount+count);
foundAtLevel.add(false);
}
else
{
levelCounter.set(index,prevCount+count);
foundAtLevel.set(index,false);
}
}
}


