Python | Word Stretch

Write a program to get all possibilities (in sorted order) for stretching a word to the given size. If the list of required indices is passed, then only those combinations present at those indices should be returned. If the required indices are null, then all the combinations have to be returned.


str = "1234"  
n = 6 
indices = null 

["111234", "112234", "112334", "112344", "122234",
 "122334", "122344", "123334", "123344", "123444"] 

The above string  “1234”, with stretch length can be stretched for different possibilities
like "111234", "112334", "112344" etc. The output represents a list of 
different stretches in a sorted manner. Since the indices are given as
null we print the entire list of possibilities 

str = "1234"
n = 6 
indices = 1, 5, 9 
["112234", "122334", "123444"]
Here only stretched strings of given indices are printed. 

str = "merit"  
n = 6 
indices = null 
["meerit", "meriit", "meritt", "merrit", "mmerit"] 

str = "java"  
n = 4 
indices = null 
Output: ["java"] 

Input : 
str = "geeksforgeeks"
n = 10 
indices = null 

since the length of the string is greater than 
the length to be stretched we return an empty index

Code : Python code for get all possibilities for stretching a word to the given size.





# function which streches the word
def WordStretch(s, n, indices) :
    res, index, result, final = [], [], [], []
    # check for corner case
    if n < len(s) :
    # check for corner case
    elif n == len(s) :
    else :
        # since the word should be streched
        # to n terms which include the length
        temp = n-len(s)
        for i in range(len(s)):
            for j in range(i, len(s)):
                if i == j :
                    l = temp * s[j]
                    m = s[:j] + l + s[j:]
                else :
                    l = (temp-1) * s[j]
                    m = s[:j] + l + s[j:]
                    # here we convert the string into list
                    # and again list into string after inserting
                    # the element in the list
                    t =[]
                    for p in m :
                    t.insert(i, s[i])
        # to sort the elements 
        # to remove the duplicate elements
        for i in result :
            if i not in final :
        # to print the entire list        
        if indices == 'null' :
        # to print particular indices    
        else :       
            index = list(map(int, indices.split()))
            for i in index :
# drive code
s = "1234"
n = 6
x = "null"
WordStretch(s, n, x)



['111234', '112234', '112334', '112344', '122234', 
'122334', '122344', '123334', '123344', '123444']

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : nidhi_biet

Article Tags :


Please write to us at to report any issue with the above content.