Asis
Asis

Reputation: 303

How to find all possible strings after deleting k number of characters from the given string?

For example we have a string s = "stackoverflow" .if we delete one character then we get "tackoverflow", "sackoverflow", "stckoverflow" ... and so on. How to get all possible strings when we delete k number of characters where k < len(s).

We can do by for loop for given exact number of characters to delete and string.But how to do it when number of characters to delete is not fixed.

Upvotes: 0

Views: 156

Answers (1)

Primusa
Primusa

Reputation: 13498

itertools is your friend:

from itertools import combinations

s = "stack overflow"
n_delete = 1

print([''.join(i) for i in combinations(s, len(s) - n_delete)])

Upvotes: 3

Related Questions