Problem · String

Get Smallest String

Learn this problem
EasyIBMFULLTIMEOA
See IBM hiring insights

Problem statement

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

getSmallestString(s: String) → String

Complete the function getSmallestString in the editor below.

getSmallestString has the following parameter:

  • s: string - a string

Returns

string: the lexicographically smallest string possible

Examples

Example 1

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

Example 2

s = "bbcad"return = "aabad"
Change bbc to aab.

Constraints

1 ≤ length of s, |s| ≤ 105

More IBM problems

drafts saved locally
public String getSmallestString(String s) {
  // write your code here
}
s"hackerrank"
expected"gackerrank"
checking account