Skip to main content
added 38 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

code to be improved to speed-up execution time Output strings from a set in lexicographical order

Puzzle Description:

You are given 'n' strings w1, w2, ......, wn. Let Si denote the set of strings formed by considering all unique substrings of the string wi. A substring is defined as a contiguous sequence of one or more characters in the string. More information on substrings can be found here. Let 'S' = {S1 U S2 U .... Sn} .i.e 'S' is a set of strings formed by considering all the unique strings in all sets S1, S2, ..... Sn. You will be given many queries and for each query and an integer 'k'. Your task is to output the lexicographically kth smallest string from the set 'S'.

Input:

The first line of input contains a single integer 'n', denoting the number of strings. Each of the next 'n' lines consists of a string. The string on the ith line (\$1 <= i<= n\$) is denoted by wi and has a length mi. The next line consists of a single integer 'q', denoting the number of queries. Each of the next 'q' lines consists of a single integer 'k'. Note: The input strings consist only of lowercase English alphabets 'a' - 'z'.

Output:

Output 'q' lines, where the ith line consists of a string which is the answer to the ith query. If the input is invalid ('k' > |S|), output "INVALID" (quotes for clarity) for that case.

Constraints:

  • \$1 <= n <= 50\$
  • \$1 <= mi <= 2000\$
  • \$1 <= q<= 500\$
  • \$1 <= k<= 1000000000\$

Sample Input:

2
aab
aac
3
3
8
23

Sample Output:

aab
c
INVALID

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".

Time-limit: 5 secs


Puzzle Description:

You are given 'n' strings w1, w2, ......, wn. Let Si denote the set of strings formed by considering all unique substrings of the string wi. A substring is defined as a contiguous sequence of one or more characters in the string. More information on substrings can be found here. Let 'S' = {S1 U S2 U .... Sn} .i.e 'S' is a set of strings formed by considering all the unique strings in all sets S1, S2, ..... Sn. You will be given many queries and for each query, you will be given an integer 'k'. Your task is to output the lexicographically kth smallest string from the set 'S'.

Input: The first line of input contains a single integer 'n', denoting the number of strings. Each of the next 'n' lines consists of a string. The string on the ith line (1<=i<=n) is denoted by wi and has a length mi. The next line consists of a single integer 'q', denoting the number of queries. Each of the next 'q' lines consists of a single integer 'k'. Note: The input strings consist only of lowercase english alphabets 'a' - 'z'.

Output: Output 'q' lines, where the ith line consists of a string which is the answer to the ith query. If the input is invalid ('k' > |S|), output "INVALID" (quotes for clarity) for that case.

Constraints:

  • 1<=n<=50
  • 1<=mi<=2000
  • 1<=q<=500
  • 1<=k<=1000000000

Sample Input:

2
aab
aac
3
3
8
23

Sample Output:

aab
c
INVALID

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".

Time-limit : 5 secs

  1. WrittenThis was written to solve puzzle only :)
  2. First I thought of creating separate sets for each string and then later unionize all them to get final mainset and then sort them using using custom comparator if necessary and later can be retrieved with index index.
  3. Later without doing all this nonsense, created tree-set which is populated with substring'ssubstrings on the run, thus combined process of unionizing,sorting and sorting.
  4. For retrieving through index I had to make an arraylistArrayList with contents of mainset. I I guess this doesn't take much time or memory than other process.

code to be improved to speed-up execution time

Puzzle Description:

You are given 'n' strings w1, w2, ......, wn. Let Si denote the set of strings formed by considering all unique substrings of the string wi. A substring is defined as a contiguous sequence of one or more characters in the string. More information on substrings can be found here. Let 'S' = {S1 U S2 U .... Sn} .i.e 'S' is a set of strings formed by considering all the unique strings in all sets S1, S2, ..... Sn. You will be given many queries and for each query, you will be given an integer 'k'. Your task is to output the lexicographically kth smallest string from the set 'S'.

Input: The first line of input contains a single integer 'n', denoting the number of strings. Each of the next 'n' lines consists of a string. The string on the ith line (1<=i<=n) is denoted by wi and has a length mi. The next line consists of a single integer 'q', denoting the number of queries. Each of the next 'q' lines consists of a single integer 'k'. Note: The input strings consist only of lowercase english alphabets 'a' - 'z'.

Output: Output 'q' lines, where the ith line consists of a string which is the answer to the ith query. If the input is invalid ('k' > |S|), output "INVALID" (quotes for clarity) for that case.

Constraints:

  • 1<=n<=50
  • 1<=mi<=2000
  • 1<=q<=500
  • 1<=k<=1000000000

Sample Input:

2
aab
aac
3
3
8
23

Sample Output:

aab
c
INVALID

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".

Time-limit : 5 secs

  1. Written to solve puzzle only :)
  2. First I thought of creating separate sets for each string and then later unionize all them to get final mainset and then sort them using custom comparator if necessary and later can be retrieved with index.
  3. Later without doing all this nonsense, created tree-set which is populated with substring's on the run, thus combined process of unionizing,sorting.
  4. For retrieving through index I had to make an arraylist with contents of mainset. I guess this doesn't take much time or memory than other process.

Output strings from a set in lexicographical order

Puzzle Description:

You are given 'n' strings w1, w2, ......, wn. Let Si denote the set of strings formed by considering all unique substrings of the string wi. A substring is defined as a contiguous sequence of one or more characters in the string. More information on substrings can be found here. Let 'S' = {S1 U S2 U .... Sn} .i.e 'S' is a set of strings formed by considering all the unique strings in all sets S1, S2, ..... Sn. You will be given many queries and for each query and an integer 'k'. Your task is to output the lexicographically kth smallest string from the set 'S'.

Input:

The first line of input contains a single integer 'n', denoting the number of strings. Each of the next 'n' lines consists of a string. The string on the ith line (\$1 <= i<= n\$) is denoted by wi and has a length mi. The next line consists of a single integer 'q', denoting the number of queries. Each of the next 'q' lines consists of a single integer 'k'. Note: The input strings consist only of lowercase English alphabets 'a' - 'z'.

Output:

Output 'q' lines, where the ith line consists of a string which is the answer to the ith query. If the input is invalid ('k' > |S|), output "INVALID" (quotes for clarity) for that case.

Constraints:

  • \$1 <= n <= 50\$
  • \$1 <= mi <= 2000\$
  • \$1 <= q<= 500\$
  • \$1 <= k<= 1000000000\$

Sample Input:

2
aab
aac
3
3
8
23

Sample Output:

aab
c
INVALID

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".

Time-limit: 5 secs


  1. This was written to solve puzzle only
  2. First I thought of creating separate sets for each string and then later unionize all them to get final mainset and then sort them using custom comparator if necessary and later can be retrieved with index.
  3. Later without doing all this nonsense, created tree-set which is populated with substrings on the run, thus combined process of unionizing and sorting.
  4. For retrieving through index I had to make an ArrayList with contents of mainset. I guess this doesn't take much time or memory than other process.
rm tag from title
Link
svick
  • 24.5k
  • 4
  • 53
  • 89

code to be improved to speed-up execution time : java

added 211 characters in body
Source Link
cypronmaya
  • 479
  • 6
  • 13
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;

public class find_str {

    private static final String INVALID = "INVALID";
    private TreeSet<String> mainset = new TreeSet<String>();
    private ArrayList<String> array = new ArrayList<String>();
    private String[] main_ary;
    private int length;
    public static void main(String args[]) {
        long start=System.currentTimeMillis();
        find_str fin = new find_str();
        Scanner scanner = new Scanner(System.in);
        int num_of_strings = scanner.nextInt();
        for (int i = num_of_strings; --i >=0;) {
            fin.process(scanner.next());
        }
        fin.initialize();
        int num_of_queries = scanner.nextInt();
        for (int i = num_of_queries; --i >=0;) {
            System.out.println(fin.query(scanner.nextInt()-1));
        }
    }

    private String query(int index) {
        if (index < length) {
            return main_ary[index];
        } else {
            return INVALID;
        }
    }

    private void process(String input) {
        int len = input.length();
        for (int i = 0; i < len; i++) {
            for (int j = i; j < len; j++) {
                mainset.add(input.substring(i, j + 1));
            }
        }
    }

    private void initialize() {
        main_ary=(String[]) mainset.toArray(new String[mainsetlength=mainset.size()]);
        length=main_arymain_ary=(String[]) mainset.length;toArray(new String[length]);
    //    array.addAll(mainset);
        
    }
}
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;

public class find_str {

    private static final String INVALID = "INVALID";
    private TreeSet<String> mainset = new TreeSet<String>();
    private ArrayList<String> array = new ArrayList<String>();
    private String[] main_ary;
    private int length;
    public static void main(String args[]) {
        long start=System.currentTimeMillis();
        find_str fin = new find_str();
        Scanner scanner = new Scanner(System.in);
        int num_of_strings = scanner.nextInt();
        for (int i = num_of_strings; --i >=0;) {
            fin.process(scanner.next());
        }
        fin.initialize();
        int num_of_queries = scanner.nextInt();
        for (int i = num_of_queries; --i >=0;) {
            System.out.println(fin.query(scanner.nextInt()-1));
        }
    }

    private String query(int index) {
        if (index < length) {
            return main_ary[index];
        } else {
            return INVALID;
        }
    }

    private void process(String input) {
        int len = input.length();
        for (int i = 0; i < len; i++) {
            for (int j = i; j < len; j++) {
                mainset.add(input.substring(i, j + 1));
            }
        }
    }

    private void initialize() {
        main_ary=(String[]) mainset.toArray(new String[mainset.size()]);
        length=main_ary.length;
    //    array.addAll(mainset);
        
    }
}
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;

public class find_str {

    private static final String INVALID = "INVALID";
    private TreeSet<String> mainset = new TreeSet<String>();
    private ArrayList<String> array = new ArrayList<String>();
    private String[] main_ary;
    private int length;
    public static void main(String args[]) {
        find_str fin = new find_str();
        Scanner scanner = new Scanner(System.in);
        int num_of_strings = scanner.nextInt();
        for (int i = num_of_strings; --i >=0;) {
            fin.process(scanner.next());
        }
        fin.initialize();
        int num_of_queries = scanner.nextInt();
        for (int i = num_of_queries; --i >=0;) {
            System.out.println(fin.query(scanner.nextInt()-1));
        }
    }

    private String query(int index) {
        if (index < length) {
            return main_ary[index];
        } else {
            return INVALID;
        }
    }

    private void process(String input) {
        int len = input.length();
        for (int i = 0; i < len; i++) {
            for (int j = i; j < len; j++) {
                mainset.add(input.substring(i, j + 1));
            }
        }
    }

    private void initialize() {
      length=mainset.size();
        main_ary=(String[]) mainset.toArray(new String[length]);
    //    array.addAll(mainset);
    }
}
added 211 characters in body
Source Link
cypronmaya
  • 479
  • 6
  • 13
Loading
deleted 21 characters in body
Source Link
cypronmaya
  • 479
  • 6
  • 13
Loading
Made the description quoted, not code.
Source Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157
Loading
Tweeted twitter.com/#!/StackCodeReview/status/152848014325723138
deleted 2 characters in body
Source Link
cypronmaya
  • 479
  • 6
  • 13
Loading
Source Link
cypronmaya
  • 479
  • 6
  • 13
Loading