2023-12-09 18:42:21 +08:00
< p > You are given an array of characters < code > letters< / code > that is sorted in < strong > non-decreasing order< / strong > , and a character < code > target< / code > . There are < strong > at least two different< / strong > characters in < code > letters< / code > .< / p >
2022-03-27 20:46:41 +08:00
2023-12-09 18:42:21 +08:00
< p > Return < em > the smallest character in < / em > < code > letters< / code > < em > that is lexicographically greater than < / em > < code > target< / code > . If such a character does not exist, return the first character in < code > letters< / code > .< / p >
2022-03-27 20:46:41 +08:00
< p > < / p >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 20:46:41 +08:00
< pre >
< strong > Input:< / strong > letters = [" c" ," f" ," j" ], target = " a"
< strong > Output:< / strong > " c"
2023-12-09 18:42:21 +08:00
< strong > Explanation:< / strong > The smallest character that is lexicographically greater than ' a' in letters is ' c' .
2022-03-27 20:46:41 +08:00
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 20:46:41 +08:00
< pre >
< strong > Input:< / strong > letters = [" c" ," f" ," j" ], target = " c"
< strong > Output:< / strong > " f"
2023-12-09 18:42:21 +08:00
< strong > Explanation:< / strong > The smallest character that is lexicographically greater than ' c' in letters is ' f' .
2022-03-27 20:46:41 +08:00
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 3:< / strong > < / p >
2022-03-27 20:46:41 +08:00
< pre >
2023-12-09 18:42:21 +08:00
< strong > Input:< / strong > letters = [" x" ," x" ," y" ," y" ], target = " z"
< strong > Output:< / strong > " x"
< strong > Explanation:< / strong > There are no characters in letters that is lexicographically greater than ' z' so we return letters[0].
2022-03-27 20:46:41 +08:00
< / pre >
< p > < / p >
< p > < strong > Constraints:< / strong > < / p >
< ul >
< li > < code > 2 < = letters.length < = 10< sup > 4< / sup > < / code > < / li >
< li > < code > letters[i]< / code > is a lowercase English letter.< / li >
< li > < code > letters< / code > is sorted in < strong > non-decreasing< / strong > order.< / li >
< li > < code > letters< / code > contains at least two different characters.< / li >
< li > < code > target< / code > is a lowercase English letter.< / li >
< / ul >