![]() |
VOOZH | about |
Given a string s, containing lowercase alphabetical characters. The task is to print all non-empty substrings of the given string.
Examples :
Input : s = "abc"
Output : "a", "ab", "abc", "b", "bc", "c"Input : s = "ab"
Output : "a", "ab", "b"Input : s = "a"
Output : "a"
Table of Content
The idea is to use two nested loops. The outer loop selects the starting index from 0 to n-1, and for each starting index, the inner loop selects the ending index from that position up to n-1.
a ab abc b bc c
The idea is to use recursion to generate all substrings. Start from index 0 and build a current string cur by adding one character at a time. After adding each character, store cur in the result list. Then recursively move to the next index to continue building longer substrings. If cur becomes empty, skip the current character and start forming substrings from the next index.
a ab abc b bc c