Print Substrings that are Prefix of the Given String - Python
Last Updated :
31 Jan, 2025
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])
Outputa
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
Outputa
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])
Outputa
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])
Outputa
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.
Similar Reads
Python | Get the string after occurrence of given substring The problem involves getting the string that is occurring after the substring has been found. Let's discuss certain ways in which this task can be performed using Python.Using partition()To extract the portion of a string that occurs after a specific substring partition() method is an efficient and
3 min read
Python - Get all substrings of given string A substring is any contiguous sequence of characters within the string. We'll discuss various methods to extract this substring from a given string by using a simple approach. Using List Comprehension :List comprehension offers a concise way to create lists by applying an expression to each element
3 min read
Finding Strings with Given Substring in List - Python The task of finding strings with a given substring in a list in Python involves checking whether a specific substring exists within any of the strings in a list. The goal is to efficiently determine if the desired substring is present in any of the elements of the list. For example, given a list a =
3 min read
Python | Remove the given substring from end of string Sometimes we need to manipulate our string to remove extra information from the string for better understanding and faster processing. Given a task in which the substring needs to be removed from the end of the string using Python. Â Â Remove the substring from the end of the string using Slicing In
3 min read
Python Program to print strings based on the list of prefix Given a Strings List, the task here is to write a python program that can extract all the strings whose prefixes match any one of the custom prefixes given in another list. Input : test_list = ["geeks", "peeks", "meeks", "leeks", "mean"], pref_list = ["ge", "ne", "me", "re"] Output : ['geeks', 'meek
5 min read