
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find list of all possible combinations of letters of a given string s in Python
Suppose we have a string s. We have to find all possible combinations of letters of s. If there are two strings with same set of characters, then show the lexicographically smallest of them. And one constraint is each character in s are unique.
So, if the input is like s = "pqr", then the output will be ['r', 'qr', 'q', 'pr', 'pqr', 'pq', 'p']
To solve this, we will follow these steps −
- st_arr := a new list
- for i in range size of s - 1 to 0, decrease by 1, do
- for j in range 0 to size of st_arr - 1, do
- insert (s[i] concatenate st_arr[j]) at the end of st_arr
- insert s[i] at the end of st_arr
- for j in range 0 to size of st_arr - 1, do
- return st_arr
Example
Let us see the following implementation to get better understanding −
def solve(s): st_arr = [] for i in range(len(s)-1,-1,-1): for j in range(len(st_arr)): st_arr.append(s[i]+st_arr[j]) st_arr.append(s[i]) return st_arr s = "pqr" print(solve(s))
Input
"pqr"
Output
['r', 'qr', 'q', 'pr', 'pqr', 'pq', 'p']
- Related Articles
- C++ Program to Generate All Possible Combinations of a Given List of Numbers
- How to find all possible permutations of a given string in Python?
- Python program to find all the Combinations in a list with the given condition
- Python – All combinations of a Dictionary List
- Python program to find all the Combinations in the list with the given condition
- JavaScript function that generates all possible combinations of a string
- How to generate a list of all possible 4 digits combinations in Excel?
- Program to find start indices of all anagrams of a string S in T in Python
- Python program to get all pairwise combinations from a list
- Print all possible combinations of r elements in a given array of size n in C++
- Python program to find all close matches of input string from a list
- Python program to find probability of getting letter 'a' in some letters and k sized combinations
- Program to find out the substrings of given strings at given positions in a set of all possible substrings in python
- Python Program to print all permutations of a given string
- Program to find total sum of all substrings of a number given as string in Python

Advertisements