
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Find all possible substrings after deleting k characters in Python
we are given a string. The required task is to take out one letter from the string and print the remaining letters in the string. And this we have to so for each letter of the string.
With loops and range
This is a basic programming approach in which we first list out the parameters required like declare the string, create variables for start and end positions and create a temporary placeholder for each of the letters. The we create a function that will iterate through each of the letters and create a string of remaining letters.
Example
list = [] def letterCombinations(s, t, start, end, index, k): if (index == k): elem = '' for j in range(k): elem += t[j] list.append(elem) return i = start while (i <= end and end - i + 1 >= k - index): temp[index] = s[i] letterCombinations(s, t, i + 1, end, index + 1, k) i += 1 stringA = 'Apple' k = 1 temp = [0] * (len(stringA) - k) start = 0 end = len(stringA) - 1 letterCombinations(stringA, temp, start, end, 0, len(stringA) - k) print(set(list))
Output
Running the above code gives us the following result −
{'pple', 'Aple', 'Appl', 'Appe'}
with itertools
In this approach we use the module itertools which has a function named combinations. That takes care of creating all possible combinations of letters after we remove one letter from the given string.
Example
from itertools import combinations stringA = 'Apple' k = 1 # using combinations res = set([''.join(i) for i in combinations(stringA, len(stringA) - k)]) print(res)
Output
Running the above code gives us the following result −
{'Appl', 'Aple', 'Appe', 'pple'}
- Related Articles
- Program to find string after deleting k consecutive duplicate characters in python
- Find K-Length Substrings With No Repeated Characters in Python
- Program to find minimum amplitude after deleting K elements in Python
- Python program to find N-sized substrings with K distinct characters
- Python – N sized substrings with K distinct characters
- Maximum possible middle element of the array after deleting exactly k elements in C++
- Program to check whether palindrome can be formed after deleting at most k characters or not in python
- Program to find maximum difference of adjacent values after deleting k numbers in python
- Find the k smallest numbers after deleting given elements in C++
- Program to find minimum possible maximum value after k operations in python
- Find the k largest numbers after deleting the given elements in C++
- Program to find out the substrings of given strings at given positions in a set of all possible substrings in python
- Find substrings that contain all vowels in Python
- Number of Substrings Containing All Three Characters in C++
- Count number of substrings with exactly k distinct characters in C++
