Python Program to Group Strings by K length Using Suffix
Last Updated :
26 Apr, 2023
Given Strings List, the task is to write a Python program to group them into K-length suffixes.
Input : test_list = ["food", "peak", "geek", "good", "weak", "sneek"], K = 3
Output : {'ood': ['food', 'good'], 'eak': ['peak', 'weak'], 'eek': ['geek', 'sneek']}
Explanation : words ending with ood are food and good, hence grouped.
Input : test_list = ["peak", "geek", "good", "weak"], K = 3
Output : {'ood': ['good'], 'eak': ['peak', 'weak'], 'eek': ['geek']}
Explanation : word ending with ood is good, hence grouped.
Method 1 : Using try/except + loop
In this, we extract the last K characters and form a string, and append it to the existing key's list corresponding to it, if not found, it goes through catch flow and creates a new key with a list with the first word initialized.
Python3
# Python3 code to demonstrate working of
# Group Strings by K length Suffix
# Using try/except + loop
# initializing list
test_list = ["food", "peak", "geek",
"good", "weak", "sneek"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 3
res = {}
for ele in test_list:
# extracting suffix
suff = ele[-K : ]
# appending if key found, else creating new one
try:
res[suff].append(ele)
except:
res[suff] = [ele]
# printing result
print("The grouped suffix Strings : " + str(res))
OutputThe original list is : ['food', 'peak', 'geek', 'good', 'weak', 'sneek']
The grouped suffix Strings : {'ood': ['food', 'good'], 'eak': ['peak', 'weak'], 'eek': ['geek', 'sneek']}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using defaultdict() + loop.
This method avoids the need of using try/except block as default list initialization is handled by defaultdict().
Python3
# Python3 code to demonstrate working of
# Group Strings by K length Suffix
# Using defaultdict() + loop
from collections import defaultdict
# initializing list
test_list = ["food", "peak", "geek",
"good", "weak", "sneek"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 3
res = defaultdict(list)
for ele in test_list:
# extracting suffix
suff = ele[-K : ]
# appending into matched suffix key
res[suff].append(ele)
# printing result
print("The grouped suffix Strings : " + str(dict(res)))
OutputThe original list is : ['food', 'peak', 'geek', 'good', 'weak', 'sneek']
The grouped suffix Strings : {'ood': ['food', 'good'], 'eak': ['peak', 'weak'], 'eek': ['geek', 'sneek']}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3 : Using for loops and endswith()
Python3
# Python3 code to demonstrate working of
# Group Strings by K length Suffix
# initializing list
test_list = ["food", "peak", "geek",
"good", "weak", "sneek"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 3
res=dict()
x=[]
for i in test_list:
if i[-K:] not in x:
x.append(i[-K:])
for i in x:
a=[]
for j in test_list:
if(j.endswith(i)):
a.append(j)
res[i]=a
print(res)
OutputThe original list is : ['food', 'peak', 'geek', 'good', 'weak', 'sneek']
{'ood': ['food', 'good'], 'eak': ['peak', 'weak'], 'eek': ['geek', 'sneek']}
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Method 4 : Using Dictionary and List Comprehensions
This approach uses a combination of dictionary and list comprehensions to group the strings based on their K-length suffix. We first create a list of all unique K-length suffixes from the strings in the input list. Then, using the list comprehension, we create a dictionary where each key is a suffix, and its value is a list of all the strings that end with that suffix.
Python3
# Python3 code to demonstrate working of
# Group Strings by K length Suffix
# initializing list
test_list = ["food", "peak", "geek",
"good", "weak", "sneek"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 3
result = {suffix: [word for word in test_list if word.endswith(suffix)] for suffix in set([word[-K:] for word in test_list])}
# printing result
print("The grouped suffix Strings : " + str(result))
OutputThe original list is : ['food', 'peak', 'geek', 'good', 'weak', 'sneek']
The grouped suffix Strings : {'ood': ['food', 'good'], 'eek': ['geek', 'sneek'], 'eak': ['peak', 'weak']}
Time Complexity: O(n * m) where n is the length of the input list test_list and m is the length of the longest string in test_list.
Auxiliary Space: O(n)
Method 5 : Using itertools.groupby()
step-by-step approach
- Import the itertools module for working with iterators.
- Sort the list of words by their last K characters using the key parameter of the sorted() function.
- Use the itertools.groupby() function to group the words by their last K characters.
- Create an empty dictionary to store the grouped suffix strings.
- Loop through the groups of words and add them to the dictionary.
- Print the final dictionary.
Python3
import itertools
# initializing list
test_list = ["food", "peak", "geek", "good", "weak", "sneek"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 3
# sort list by last K characters
sorted_list = sorted(test_list, key=lambda word: word[-K:])
# group words by suffix using itertools.groupby()
groups = itertools.groupby(sorted_list, key=lambda word: word[-K:])
# create empty dictionary to store grouped suffix strings
result = {}
# loop through groups and add them to dictionary
for suffix, words in groups:
result[suffix] = list(words)
# printing result
print("The grouped suffix Strings : " + str(result))
OutputThe original list is : ['food', 'peak', 'geek', 'good', 'weak', 'sneek']
The grouped suffix Strings : {'eak': ['peak', 'weak'], 'eek': ['geek', 'sneek'], 'ood': ['food', 'good']}
The time complexity of this method is O(n*log(n)), where n is the length of the original list, due to the sorting operation.
The auxiliary space used by this method is O(n), where n is the length of the original list, due to the creation of the sorted list.
Similar Reads
Python program to remove K length words in String Given a String, write a Python program to remove all the words with K length. Examples: Input : test_str = 'Gfg is best for all geeks', K = 3 Output : is best geeks Explanation : Gfg, for and all are of length 3, hence removed. Input : test_str = 'Gfg is best for all geeks', K = 2 Output : Gfg best
5 min read
Python program to extract numeric suffix from string Given a string of characters with digits embedded in it. The task is to write a Python program to extract all the numbers that are trailing, i.e at the suffix of the string. Examples:Input : test_str = "GFG04"Output : 04Explanation : 04 is suffix of string and number hence extracted.Input : test_str
7 min read
Python program to Increment Suffix Number in String Given a String, the task is to write a Python program to increment the number which is at end of the string. Input : test_str = 'geeks006' Output : geeks7 Explanation : Suffix 006 incremented to 7. Input : test_str = 'geeks007' Output : geeks8 Explanation : Suffix 007 incremented to 8. Method #1 : U
5 min read
Python Program To Find Longest Common Prefix Using Sorting Problem Statement: Given a set of strings, find the longest common prefix.Examples: Input: {"geeksforgeeks", "geeks", "geek", "geezer"} Output: "gee" Input: {"apple", "ape", "april"} Output: "ap" The longest common prefix for an array of strings is the common prefix between 2 most dissimilar strings
2 min read
Python Program to split string into k sized overlapping strings Given a string, the task is to write a Python program to extract overlapping consecutive string slices from the original string according to size K. Example: Input : test_str = 'Geeksforgeeks', K = 4 Output : ['Geek', 'eeks', 'eksf', 'ksfo', 'sfor', 'forg', 'orge', 'rgee', 'geek', 'eeks'] Explanati
4 min read
Python - Possible Substring count from String Given target string and argument substring, count how many substrings can be constructed using string characters, repetitions not allowed. Input : test_str = "geksefokesgergeeks", arg_str = "geeks" Output : 3 Explanation : "geeks" can be created 3 times using string characters. Input : test_str = "g
4 min read
Python program to Concatenate Kth index words of String Given a string with words, concatenate the Kth index of each word. Input : test_str = 'geeksforgeeks best geeks', K = 3 Output : ktk Explanation : 3rd index of "geeksforgeeks" is k, "best" has 't' as 3rd element. Input : test_str = 'geeksforgeeks best geeks', K = 0 Output : gbg Method #1 : Using joi
4 min read
Python | Split list of strings into sublists based on length Given a list of strings, write a Python program to split the list into sublists based on string length. Examples: Input : ['The', 'art', 'of', 'programming'] Output : [['of'], ['The', 'art'], ['programming']] Input : ['Welcome', 'to', 'geeksforgeeks'] Output : [['to'], ['Welcome'], ['geeksforgeeks']
3 min read
Python - Filter String Tuples if String lengths equals K Given List of tuples, filter tuples, whose element strings have length equal to K. Input : test_list = [("ABC", "Gfg", "CS1"), ("Gfg", "Best"), ("Gfg", "WoOW")], K = 3 Output : [('ABC', 'Gfg', 'CS1')] Explanation : All Strings have length 3 in above tuple. Input : test_list = [("ABCD", "Gfg", "CS1")
6 min read