Problem Statement: Unique Substrings
You are given a string, s. Your task is to find and print all unique substrings of length greater than 1 in alphabetical order of increasing length.
Input Format:
A single line containing a string, s, consisting of lowercase English letters only.
Output Format:
Print the number of unique substrings followed by the substrings themselves, each on a new line.
Constraints:
2 ≤ Length of s ≤ 1000
Sample Input:
msdhoni
Sample Output:
21
dh
dho
dhon
dhoni
ho
hon
honi
ms
msd
msdh
msdho
msdhon
msdhoni
ni
on
oni
sd
sdh
sdho
sdhon
sdhoni
Explanation:
In the given string "msdhoni", the unique substrings of length greater than 1 are printed in alphabetical order of increasing length.
Test Cases:
code
6
co
cod
code
de
od
ode
csk
3
cs
csk
sk
SOLUTION:
def unique_substrings(s):
substrings = set()
n = len(s)
for length in range(2, n + 1):
for i in range(n - length + 1):
res = s[i:i + length]
substrings.add(res)
substrings = sorted(substrings)
print(len(substrings))
print()
for i in substrings:
print(i)
s = input()
unique_substrings(s)
Insights:
- The function utilizes a set substrings to store unique substrings. This ensures that duplicate substrings are not included in the final output.
- It employs nested loops to iterate over all possible substrings of different lengths. The outer loop iterates over the lengths of substrings, while the inner loop generates substrings of the current length.
- The range for the outer loop starts from 2 and goes up to n + 1, where n is the length of the input string s. This ensures that substrings of at least length 2 are considered.
- Substrings are generated using slicing s[i:i + length], where i is the starting index and length is the current length being considered.
- Each generated substring is added to the substrings set, ensuring uniqueness.
- After generating all unique substrings, the function sorts them alphabetically using the sorted function. This step ensures that the substrings are printed in alphabetical order.
- The function prints the count of unique substrings first, indicating the total number of unique substrings found. Finally, it iterates over the sorted substrings and prints each substring on a new line.
Comments
Post a Comment