Skip to main content
better title; quote challenge text; add programming-challenge tag
Source Link
Gareth Rees
  • 50.1k
  • 3
  • 130
  • 211

Can the below code be optimized to find the list of substrings in less than O(n^2) complexity The Substring Game! challenge

This is athe challengeSubstring Game! challenge from HackerEarth.:

Watson gives Sherlock a string. Watson calculates all the substrings of S in his favourite order.

Watson gives Sherlock a string (let's denote it by S). Watson calculates all the substrings of S in his favourite order.

According to the Watson's favourite order, all the substrings starting from first character of the string will occur first in the sorted order of their length, followed by all the substrings starting from the second character of the string in the sorted order of their length, and so on.

According to the Watson's favourite order, all the substrings starting from first character of the string will occur first in the sorted order of their length, followed by all the substrings starting from the second character of the string in the sorted order of their length, and so on.

Watson will ask Sherlock 'q' questions. In each question, Watson will give Sherlock a number and Sherlock has to speak the substring on that number. If there is no possible substring, then Sherlock has to speak -1.

For example, suppose S = abc. Then, all the substrings of S as per Watson's favourite order are:

  1. a
  2. ab
  3. abc
  4. b
  5. bc
  6. c

Watson will ask Sherlock 'q' questions. In each question, Watson will give Sherlock a number and Sherlock has to speak the substring on that number. If there is no possible substring, then Sherlock has to speak -1.

Below is myCan the below code please let me know what changes can be doneimproved to run in \$o(n^2)\$ complexity?

str=raw_input()
q=int(raw_input())
num=raw_input().split(' ')
substr=[]
for i,s in enumerate(str):
    #print i,s
    for j in range(i+1,len(str)+1):
        #print j
        substr.append(s+str[i+1:j])
#print substr
for i in range(0,q):
    try:
       print substr[int(num[i])-1]
    except IndexError:
       print -1

Sample Input
First line is the string - abc
Second line is the number of substrings asked- 2
Third line is the position of the substrings to return from the list of substrings- 2 5

The list of substrings would be ['a','ab','abc','b','bc','c']
Sample Output
ab
bc

Can the below code be optimized to find the list of substrings in less than O(n^2) complexity

This is a challenge from HackerEarth.

Watson gives Sherlock a string. Watson calculates all the substrings of S in his favourite order.

According to the Watson's favourite order, all the substrings starting from first character of the string will occur first in the sorted order of their length, followed by all the substrings starting from the second character of the string in the sorted order of their length, and so on.

Watson will ask Sherlock 'q' questions. In each question, Watson will give Sherlock a number and Sherlock has to speak the substring on that number. If there is no possible substring, then Sherlock has to speak -1.

Below is my code please let me know what changes can be done

str=raw_input()
q=int(raw_input())
num=raw_input().split(' ')
substr=[]
for i,s in enumerate(str):
    #print i,s
    for j in range(i+1,len(str)+1):
        #print j
        substr.append(s+str[i+1:j])
#print substr
for i in range(0,q):
    try:
       print substr[int(num[i])-1]
    except IndexError:
       print -1

Sample Input
First line is the string - abc
Second line is the number of substrings asked- 2
Third line is the position of the substrings to return from the list of substrings- 2 5

The list of substrings would be ['a','ab','abc','b','bc','c']
Sample Output
ab
bc

The Substring Game! challenge

This is the Substring Game! challenge from HackerEarth:

Watson gives Sherlock a string (let's denote it by S). Watson calculates all the substrings of S in his favourite order.

According to the Watson's favourite order, all the substrings starting from first character of the string will occur first in the sorted order of their length, followed by all the substrings starting from the second character of the string in the sorted order of their length, and so on.

For example, suppose S = abc. Then, all the substrings of S as per Watson's favourite order are:

  1. a
  2. ab
  3. abc
  4. b
  5. bc
  6. c

Watson will ask Sherlock 'q' questions. In each question, Watson will give Sherlock a number and Sherlock has to speak the substring on that number. If there is no possible substring, then Sherlock has to speak -1.

Can the below code be improved to run in \$o(n^2)\$ complexity?

str=raw_input()
q=int(raw_input())
num=raw_input().split(' ')
substr=[]
for i,s in enumerate(str):
    #print i,s
    for j in range(i+1,len(str)+1):
        #print j
        substr.append(s+str[i+1:j])
#print substr
for i in range(0,q):
    try:
       print substr[int(num[i])-1]
    except IndexError:
       print -1

Sample Input
First line is the string - abc
Second line is the number of substrings asked- 2
Third line is the position of the substrings to return from the list of substrings- 2 5

The list of substrings would be ['a','ab','abc','b','bc','c']
Sample Output
ab
bc

added 187 characters in body
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

This is a challenge from HackerEarth.

Watson gives Sherlock a string. Watson calculates all the substrings of S in his favourite order.

According to the Watson's favourite order, all the substrings starting from first character of the string will occur first in the sorted order of their length, followed by all the substrings starting from the second character of the string in the sorted order of their length, and so on.

Watson will ask Sherlock 'q' questions. In each question, Watson will give Sherlock a number and Sherlock has to speak the substring on that number. If there is no possible substring, then Sherlock has to speak -1.

Below is my code please let me know what changes can be done

str=raw_input()
q=int(raw_input())
num=raw_input().split(' ')
substr=[]
for i,s in enumerate(str):
    #print i,s
    for j in range(i+1,len(str)+1):
        #print j
        substr.append(s+str[i+1:j])
#print substr
for i in range(0,q):
    try:
       print substr[int(num[i])-1]
    except IndexError:
       print -1

Sample Input
First line is the string - abc
Second line is the number of substrings asked- 2
Third line is the position of the substrings to return from the list of substrings- 2 5

The list of substrings would be ['a','ab','abc','b','bc','c']
Sample Output
ab
bc

Watson gives Sherlock a string. Watson calculates all the substrings of S in his favourite order.

According to the Watson's favourite order, all the substrings starting from first character of the string will occur first in the sorted order of their length, followed by all the substrings starting from the second character of the string in the sorted order of their length, and so on.

Watson will ask Sherlock 'q' questions. In each question, Watson will give Sherlock a number and Sherlock has to speak the substring on that number. If there is no possible substring, then Sherlock has to speak -1.

Below is my code please let me know what changes can be done

str=raw_input()
q=int(raw_input())
num=raw_input().split(' ')
substr=[]
for i,s in enumerate(str):
    #print i,s
    for j in range(i+1,len(str)+1):
        #print j
        substr.append(s+str[i+1:j])
#print substr
for i in range(0,q):
    try:
       print substr[int(num[i])-1]
    except IndexError:
       print -1

Sample Input
First line is the string - abc
Second line is the number of substrings asked- 2
Third line is the position of the substrings to return from the list of substrings- 2 5

The list of substrings would be ['a','ab','abc','b','bc','c']
Sample Output
ab
bc

This is a challenge from HackerEarth.

Watson gives Sherlock a string. Watson calculates all the substrings of S in his favourite order.

According to the Watson's favourite order, all the substrings starting from first character of the string will occur first in the sorted order of their length, followed by all the substrings starting from the second character of the string in the sorted order of their length, and so on.

Watson will ask Sherlock 'q' questions. In each question, Watson will give Sherlock a number and Sherlock has to speak the substring on that number. If there is no possible substring, then Sherlock has to speak -1.

Below is my code please let me know what changes can be done

str=raw_input()
q=int(raw_input())
num=raw_input().split(' ')
substr=[]
for i,s in enumerate(str):
    #print i,s
    for j in range(i+1,len(str)+1):
        #print j
        substr.append(s+str[i+1:j])
#print substr
for i in range(0,q):
    try:
       print substr[int(num[i])-1]
    except IndexError:
       print -1

Sample Input
First line is the string - abc
Second line is the number of substrings asked- 2
Third line is the position of the substrings to return from the list of substrings- 2 5

The list of substrings would be ['a','ab','abc','b','bc','c']
Sample Output
ab
bc

edited tags
Link
Peilonrayz
  • 44.6k
  • 7
  • 80
  • 158
Added the problem statement
Source Link
katty
  • 615
  • 4
  • 12
Loading
Source Link
katty
  • 615
  • 4
  • 12
Loading