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.
Examples:
Input:
str = "1234"
n = 6
indices = null
Output:
["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
Input:
str = "1234"
n = 6
indices = 1, 5, 9
Output:
["112234", "122334", "123444"]
Here only stretched strings of given indices are printed.
Input:
str = "merit"
n = 6
indices = null
Output:
["meerit", "meriit", "meritt", "merrit", "mmerit"]
Input:
str = "java"
n = 4
indices = null
Output: ["java"]
Input :
str = "geeksforgeeks"
n = 10
indices = null
Output:
[]
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.
Python3
def WordStretch(s, n, indices) :
res, index, result, final = [], [], [], []
if n < len (s) :
print ([])
elif n = = len (s) :
print ([s])
else :
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:]
result.append(m)
else :
l = (temp - 1 ) * s[j]
m = s[:j] + l + s[j:]
t = []
for p in m :
t.append(p)
t.insert(i, s[i])
result.append("".join(t))
result.sort()
for i in result :
if i not in final :
final.append(i)
if indices = = 'null' :
print (result)
else :
index = list ( map ( int , indices.split()))
for i in index :
res.append(final[i])
print (res)
s = "1234"
n = 6
x = "null"
WordStretch(s, n, x)
|
Output:
['111234', '112234', '112334', '112344', '122234',
'122334', '122344', '123334', '123344', '123444']
Last Updated :
01 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...