FastPrepFind Lexicographically Smallest String

Find Lexicographically Smallest String

Amazon logoAmazon● HardFULLTIMEOA
Learn

Problem statement

Given a lowercase string s, find the lexicographically smallest lowercase string t that has the same length as s, is strictly greater than s, and has no two equal adjacent characters.

Lexicographic order uses the usual alphabet order from a through z. Return "-1" if no such string exists.

Function

findLexicographicallySmallestString(s: String) → String

Examples

Example 1

s = "abbcd"return = "abcab"

The string "abcab" is greater than "abbcd", has the same length, and has no equal adjacent characters. Any smaller string greater than "abbcd" either keeps the invalid adjacent bb or cannot be completed validly.

Constraints

  • s consists only of lowercase English letters from a through z.

More Amazon problems

See Amazon hiring insights
public String findLexicographicallySmallestString(String s) {
  // write your code here
}
s"abbcd"
expected"abcab"
Checking account…