FastPrepFriendship String

Friendship String

Rubrik logoRubrik● MediumOA
Learn

Problem statement

You are given a string s containing decimal digits. In one operation, choose a digit d, remove it, and insert min(d + 1, 9) at any position.

Perform any number of operations and return the lexicographically smallest string obtainable. Leading zeroes are allowed.

Function

friendshipString(s: String) → String

Examples

Example 1

s = "415863388791991"return = "111445678999999"

A digit that has a smaller digit to its right is moved and incremented once, capped at 9. The other digits stay unchanged. Sorting the resulting digits gives 111445678999999.

Constraints

  • 1 <= s.length <= 2 * 10^5
  • s contains only decimal digits.

More Rubrik problems

See Rubrik hiring insights
public String friendshipString(String s) {
  // write your code here
}
s"415863388791991"
expected"111445678999999"
Checking account…