Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

String Permutations Program

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is String Permutations Program. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

In i2c configuration tooling, a string may represent selectable characters in a payment or cardholder rule. Given a string and an arrangement mode, generate every unique arrangement without returning duplicates caused by repeated characters.

Implement a function that supports two modes:

  1. "permutations": return every distinct arrangement using all characters exactly once.
  2. "combinations": return every distinct non-empty combination of the characters, where character order within a combination is nondecreasing. Each character occurrence may be used at most once, so repeated input characters are treated as a multiset.

Return results in lexicographic order. For combinations, include combinations of every possible length.

Formal Specification

  • Input: a string s containing lowercase English letters and a string mode, either "permutations" or "combinations".
  • Output: a list of unique strings sorted lexicographically.
  • The input string may contain repeated characters.

Constraints

  • 0 <= len(s) <= 9
  • s contains only lowercase English letters
  • mode is either "permutations" or "combinations"
  • Results must contain no duplicates
  • Results must be sorted lexicographically

Function Signature

def generate_arrangements(s, mode):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output