Open In App

Print Substrings that are Prefix of the Given String – Python

Last Updated : 31 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of printing the substrings that are prefixes of a given string involves generating all the possible substrings that start from the first character of the string and end at any subsequent position.
For example, if the given string is s = “hello”, the prefixes of the string would include “h”, “he”, “hel”, “hell”, and “hello”. We want to print these prefixes in order, starting from the smallest to the largest with the entire string being the final prefix.

Using Slicing

Slicing allows us to extract any part of the string from the start to any given position. This enables us to progressively generate prefixes by increasing the substring length from the start to the full string.

Python
s = "abdabc"    
for i in range(1, len(s) + 1):
    print(s[:i])

Output
a
ab
abd
abda
abdab
abdabc

Using sliding window

Sliding window extract substrings of different sizes and checks if each substring matches the prefix of the string. It expands the window step by step, performing comparisons for each window size. This method involves slicing and checking substrings at each step.

Python
s = "ababc"
n = 1 # Window size

# Loop through all window sizes
while n <= len(s):
  
    # Check for prefix matches
    for i in range(len(s) - n + 1):
      
        if s[i:i+n] == s[:n]:
            print(s[i:i+n])
    n += 1  # Increase window size

Output
a
a
ab
ab
aba
abab
ababc

Explanation:

  • while n <= len(s) start a loop that iterates over all possible window sizes from 1 to the length of s .
  • for i in range(len(s) – n + 1) for each window size, slide through the string s to check substrings.
  • if s[i:i+n] == s[:n] compare the current substring to the prefix (first n characters) of the s .
  • print(s[i:i+n]) if a match is found then print the substring.

Using string comparison

String comparison finds prefix substrings by slicing the string at each position. It iterates through the string comparing each substring with its corresponding prefix. This approach is simple way to extract and print all prefixes of a string.

Python
s = "ababc"
for i in range(len(s)):
  
    if s[:i+1] != s[:i]:
        print(s[:i+1])

Output
a
ab
aba
abab
ababc

Explanation:

  • for i in range(len(s)) loop goes through each character in s, starting from the first index.
  • if s[:i+1] != s[:i] checks if the current prefix (s[:i+1]) is different from the previous one (s[:i]) and ensures that duplicates are avoided by only printing when a new prefix appears.
  • print(s[:i+1]) prints the current prefix substring.

Using hash set

This method uses a hash set to store unique prefixes of a string. As it loops through the string it checks each prefix and prints it only if it hasn’t been seen before. The use of the set ensures that duplicates are avoided efficiently.

Python
s = "ababc"
p = set() 

# Iterate through the `s`
for i in range(1, len(s) + 1):
  
    if s[:i] not in p:
        print(s[:i])  
        p.add(s[:i])

Output
a
ab
aba
abab
ababc

Explanation:

  • if s[:i] not in p checks if the current prefix s[:i] is not already in the set p.
  • print(s[:i]) prints the unique prefix.
  • p.add(s[:i]) adds the current prefix to the set p to ensure no duplicates are printed.


Next Article

Similar Reads