String Permutations

MEDIUM

Implement a function that generates all distinct permutations of a given string. The function should return a list of these permutations. If the input string contains duplicate characters, the permutations should be unique.

Examples:

Example 1:

Input: s = "abc"
Output: ["abc", "acb", "bac", "bca", "cab", "cba"] (Order doesn't matter)

Example 2:

Input: s = "aab"
Output: ["aab", "aba", "baa"] (Unique permutations only)

Example 3:

Input: s = "a"
Output: ["a"]

Constraints:

  • 1 <= s.length <= 8 (String length is usually small for permutation problems due to N! complexity).
  • s contains lowercase English letters.

Function Signature (Python):

from typing import List

class Solution:
    def permuteString(self, s: str) -> List[str]:
        # Your code here
        pass

 

Nerchuko Academy · Free DS Interview Prep