Created
September 17, 2012 04:42
-
-
Save strongme/3735607 to your computer and use it in GitHub Desktop.
Explanation: For the sample test case, we have 2 strings "aab" and "aac". S1 = {"a", "aa", "aab", "ab", "b"} . These are the 5 unique substrings of "aab". S2 = {"a", "aa", "aac", "ac", "c" } . These are the 5 unique substrings of "aac". Now, S = {S1 U S
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.TreeSet; | |
public class Solution { | |
public Set<String> getSubStrings(String s,Set<String> result) { | |
for(int i=1;i<=s.length();i++) { | |
for(int j=0;j<s.length();j++) { | |
int count = i; | |
int tmpIndex = j; | |
String res = ""; | |
while(count>0) { | |
if(tmpIndex>=s.length()) | |
break; | |
res += s.charAt(tmpIndex++); | |
count--; | |
} | |
result.add(res); | |
} | |
} | |
return result; | |
} | |
public static void main(String[] args) throws IOException { | |
Solution solution = new Solution(); | |
int n = Integer.valueOf(new BufferedReader(new InputStreamReader(System.in)).readLine()); | |
String[] data = new String[n]; | |
Set<String> sub = new TreeSet<String>(); | |
for(int i=0;i<n;i++) { | |
data[i] = new BufferedReader(new InputStreamReader(System.in)).readLine(); | |
} | |
int q = Integer.valueOf(new BufferedReader(new InputStreamReader(System.in)).readLine()); | |
int[] k = new int[q]; | |
for(int i=0;i<q;i++) { | |
k[i] = Integer.valueOf(new BufferedReader(new InputStreamReader(System.in)).readLine()); | |
} | |
for(int i=0;i<n;i++) { | |
sub = solution.getSubStrings(data[i], sub); | |
} | |
List<String> inList = new ArrayList<String>(sub); | |
int size = inList.size(); | |
for(int i : k) { | |
if(i<=size&&i>0) { | |
System.out.println(inList.get(i-1)); | |
}else { | |
System.out.println("INVALID"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Explanation:
For the sample test case, we have 2 strings "aab" and "aac".
S1 = {"a", "aa", "aab", "ab", "b"} . These are the 5 unique substrings of "aab".
S2 = {"a", "aa", "aac", "ac", "c" } . These are the 5 unique substrings of "aac".
Now, S = {S1 U S2} = {"a", "aa", "aab", "aac", "ab", "ac", "b", "c"}. Totally, 8 unique strings are present in the set 'S'.
The lexicographically 3rd smallest string in 'S' is "aab" and the lexicographically 8th smallest string in 'S' is "c". Since there are only 8 distinct substrings, the answer to the last query is "INVALID".
I just can not pass the test case
https://www.interviewstreet.com/challenges/dashboard/#problem/4efa210eb70ac