FastPrepGenerate Strings of Length at Least Three

Generate Strings of Length at Least Three

Morgan Stanley logoMorgan Stanley● MediumINTERNONSITE INTERVIEW
Learn

Problem statement

Given a string of distinct lowercase characters, generate every string formed without reusing a character whose length is at least three and at most the input length.

Return the strings in lexicographic order.

Function

generateStrings(characters: String) → String[]

Examples

Example 1

characters = "abc"return = ["abc","acb","bac","bca","cab","cba"]

Case 1 exercises the documented deterministic contract.

Example 2

characters = "abcd"return = ["abc","abcd","abd","abdc","acb","acbd","acd","acdb","adb","adbc","adc","adcb","bac","bacd","bad","badc","bca","bcad","bcd","bcda","bda","bdac","bdc","bdca","cab","cabd","cad","cadb","cba","cbad","cbd","cbda","cda","cdab","cdb","cdba","dab","dabc","dac","dacb","dba","dbac","dbc","dbca","dca","dcab","dcb","dcba"]

Case 2 exercises the documented deterministic contract.

Example 3

characters = "cba"return = ["abc","acb","bac","bca","cab","cba"]

Case 3 exercises the documented deterministic contract.

Constraints

  • 3 <= characters.length <= 7.
  • All characters are distinct lowercase English letters.

More Morgan Stanley problems

See Morgan Stanley hiring insights
public String[] generateStrings(String characters) {
    // Write your code here.
}
characters"abc"
expected["abc", "acb", "bac", "bca", "cab", "cba"]
Checking account…