Lexicographically next string (original) (raw)

Last Updated : 2 Sep, 2025

Given a string **s, find the smallest string that is strictly greater than s in **lexicographic order.

**Examples:

**Input: s = "geeks"
**Output: "geekt"
**Explanation: The last character 's' becomes 't', forming the next lexicographic string.

**Input: s = "raavz"
**Output: "raaw"
**Explanation: Last character is 'z', so we move left to 'v' and increment it to 'w' and remove the rightmost 'z' to make next string.

**Input: s = "zzz"
**Output: "zzza"
**Explanation: All characters are 'z', so we append 'a' to make the next string.

[Approach] Greedy rightmost Increment – O(n) Time and O(1) Space

We scan the string from right to left to locate the first character that is not 'z'. Increasing this character by one gives the next string with the least change.
If the entire string is made of 'z', no character can be increased, so we simply append 'a' at the end.

**Steps to implement the above idea:

#include #include using namespace std;

string nextString(string &s) { int i = s.length() - 1;

// Move left while characters are 'z'
while (i >= 0 && s[i] == 'z') {
    i--;
}

// If all characters were 'z', append
// 'a' to the string
if (i == -1) {
    return s + 'a';
}

// Otherwise, increment the 
// rightmost non-'z' character
s[i]++;

// Erase all characters after 
// the incremented one
s.erase(i + 1);
return s;

}

int main() { string s = "geeks"; cout << nextString(s); return 0; }

Java

class GfG { static String nextString(String s) { int i = s.length() - 1;

    // Move left while characters are 'z'
    while (i >= 0 && s.charAt(i) == 'z') {
        i--;
    }

    // If all characters were 'z', append
    // 'a' to the string
    if (i == -1) {
        return s + 'a';
    }

    // Otherwise, increment the 
    // rightmost non-'z' character
    char[] arr = s.toCharArray();
    arr[i]++;

    // Erase all characters after 
    // the incremented one
    return new String(arr, 0, i + 1);
}

public static void main(String[] args) {
    String s = "geeks";
    System.out.println(nextString(s));
}

}

Python

def nextString(s: str) -> str: i = len(s) - 1

# Move left while characters are 'z'
while i >= 0 and s[i] == 'z':
    i -= 1

# If all characters were 'z', append 'a'
if i == -1:
    return s + 'a'

# Otherwise, increment the rightmost non-'z' character
arr = list(s)
arr[i] = chr(ord(arr[i]) + 1)

# Return only up to the incremented character
return "".join(arr[:i + 1])

if name == "main": s = "geeks" print(nextString(s))

C#

using System;

class GfG { static string nextString(string s) { int i = s.Length - 1;

    // Move left while characters are 'z'
    while (i >= 0 && s[i] == 'z') {
        i--;
    }

    // If all characters were 'z', append
    // 'a' to the string
    if (i == -1) {
        return s + 'a';
    }

    // Otherwise, increment the 
    // rightmost non-'z' character
    char[] arr = s.ToCharArray();
    arr[i]++;

    // Erase all characters after 
    // the incremented one
    return new string(arr, 0, i + 1);
}

static void Main() {
    string s = "geeks";
    Console.WriteLine(nextString(s));
}

}

JavaScript

function nextString(s) { let i = s.length - 1;

// Move left while characters are 'z'
while (i >= 0 && s[i] === 'z') {
    i--;
}

// If all characters were 'z', append
// 'a' to the string
if (i === -1) {
    return s + 'a';
}

// Otherwise, increment the 
// rightmost non-'z' character
let arr = s.split('');
arr[i] = String.fromCharCode(arr[i].charCodeAt(0) + 1);

// Erase all characters after 
// the incremented one
return arr.slice(0, i + 1).join('');

}

// Driver Code let s = "geeks"; console.log(nextString(s));

`