All substrings

Easy
0/40
85 upvotes
Asked in company
Oracle

Problem statement

For a given input string(str), write a function to print all the possible substrings.

Substring
A substring is a contiguous sequence of characters within a string. 
Example: "cod" is a substring of "coding". Whereas "cdng" is not as the characters taken are not contiguous
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first and only line of input contains a string without any leading and trailing spaces. All the characters in the string would be in lower case.
Output Format:
Print all the substrings possible, where every substring is printed on a separate line.
Note:
The order in which the substrings are printed does not matter.
Constraints:
0 <= N <= 10^3
Where N is the length of the input string.

Time Limit: 1 second
Sample Input 1:
abc
Sample Output 1:
a 
ab 
abc 
b 
bc 
c 
 Sample Input 2:
co
Sample Output 2:
c 
co 
o
Approaches (2)
3 nested loops
  • We maintain 3 variables i, j, k.
  • i is for the starting index of the substring.
  • j is for the ending index of the substring.
  • Finally, we iterate k from i to j and print each character of the substring.
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
All substrings
Full screen
Console