FastPrepFastPrep
Problem Brief

Get Smallest String

FULLTIMEOA
See IBM online assessment and hiring insights

Given a string s that consists of lowercase English letters, select exactly one non-empty substring of s and replace each character of it with the previous character of the English alphabet. For example 'b' is converted to 'a', 'c' is converted to 'b'; ..., and 'a' is converted to 'z'.

Find the lexicographically smallest string that can be obtained after performing the above operation exactly once.

Function Description

Complete the function getSmallestString in the editor below.

getSmallestString has the following parameter:

  • s: string - a string

Returns

string: the lexicographically smallest string possible

1Example 1

Input
s = "hackerrank"
Output
"gackerrank"
Explanation
Select and change only the first character. Return "gackerrank", the lexicographically smallest string possible.

2Example 2

Input
s = "bbcad"
Output
"aabad"
Explanation
Change bbc to aab.

Constraints

Limits and guarantees your solution can rely on.

1 ≤ length of s, |s| ≤ 105
public String getSmallestString(String s) {
  // write your code here
}
Input

s

"hackerrank"

Output

"gackerrank"

Sign in to submit your solution.