Answers for "print all combinations of a string"

4

python print combinations of string

test_str = "abc"
res = [test_str[i: j] for i in range(len(test_str)) 
          for j in range(i + 1, len(test_str) + 1)]
print(res)#['a', 'ab', 'abc', 'b', 'bc', 'c']
Posted by: Guest on May-26-2021
3

python print combinations of string

import itertools
 
if __name__ == '__main__':
 
    nums = list("ABC")
    permutations = list(itertools.permutations(nums))
 
    # Output: ['ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA']
    print([''.join(permutation) for permutation in permutations])
Posted by: Guest on May-26-2021
6

find all permutations of a string

void permute(string a, int l, int r)  
{  
    // Base case  
    if (l == r)  
        cout<<a<<endl;  
    else
    {  
        // Permutations made  
        for (int i = l; i <= r; i++)  
        {  
  
            // Swapping done  
            swap(a[l], a[i]);  
  
            // Recursion called  
            permute(a, l+1, r);  
  
            //backtrack  
            swap(a[l], a[i]);  
        }  
    }  
}
Posted by: Guest on May-01-2020
0

how to print all permutations of a string

void permutation(string s)
{
    sort(s.begin(),s.end());
	do{
		cout << s << " ";
	}
    while(next_permutation(s.begin(),s.end()); // std::next_permutation
    
    cout << endl;
}
Posted by: Guest on September-29-2020

Code answers related to "print all combinations of a string"

Python Answers by Framework

Browse Popular Code Answers by Language