Backend Engineer
Senior
programming
Given a string, return all possible permutations of that string. The permutations should not include duplicates.
Constraints:
- The input string will have at most 8 characters.
Examples:
1. Input: 'abc'
Output: ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
2. Input: 'aab'
Output: ['aab', 'aba', 'baa']
```
def permute(s):
from itertools import permutations
return sorted(set([''.join(p) for p in permutations(s)]))
# Example usages:
print(permute('abc')) # ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
print(permute('aab')) # ['aab', 'aba', 'baa']
```
Trusted by 100+ professionals preparing for interviews
Trusted by 100+ professionals
50+ Company Question Banks
5+ Supported Languages
Practice More Questions Like This
Generate unlimited interview questions with structured answers, code runner, and AI-powered walkthroughs.
Get Started Free