Skip to content
Related Articles

Related Articles

Improve Article

Lexicographically next string

  • Difficulty Level : Easy
  • Last Updated : 13 May, 2021

Given a string, find lexicographically next string.
Examples: 
 

Input : geeks
Output : geekt
The last character 's' is changed to 't'.

Input : raavz
Output : raawz
Since we can't increase last character, 
we increment previous character.

Input :  zzz
Output : zzza

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

If string is empty, we return ‘a’. If string contains all characters as ‘z’, we append ‘a’ at the end. Otherwise we find first character from end which is not z and increment it.
 

C++




// C++ program to find lexicographically next
// string
#include <bits/stdc++.h>
using namespace std;
 
string nextWord(string s)
{
    // If string is empty.
    if (s == "")
        return "a";
 
    // Find first character from right
    // which is not z.
     
    int i = s.length() - 1;
    while (s[i] == 'z' && i >= 0)
        i--;
 
    // If all characters are 'z', append
    // an 'a' at the end.
    if (i == -1)
        s = s + 'a';
 
    // If there are some non-z characters
    else
        s[i]++;
 
    return s;
}
 
// Driver code
int main()
{
    string str = "samez";
    cout << nextWord(str);
    return 0;
}

Java




// Java program to find
// lexicographically next string
import java.util.*;
 
class GFG
{
public static String nextWord(String str)
{
     
    // if string is empty
    if (str == "")
    return "a";
     
    // Find first character from
    // right which is not z.
    int i = str.length() - 1;
    while (str.charAt(i) == 'z' && i >= 0)
        i--;
         
    // If all characters are 'z',
    // append an 'a' at the end.
    if (i == -1)
        str = str + 'a';
 
// If there are some
// non-z characters
else
    str = str.substring(0, i) +
         (char)((int)(str.charAt(i)) + 1) +
                      str.substring(i + 1);
return str;
}
 
// Driver Code
public static void main (String[] args)
{
    String str = "samez";
    System.out.print(nextWord(str));
}
}
 
// This code is contributed
// by Kirti_Mangal

Python3




# Python 3 program to find lexicographically
# next string
 
def nextWord(s):
     
    # If string is empty.
    if (s == " "):
        return "a"
 
    # Find first character from right
    # which is not z.
    i = len(s) - 1
    while (s[i] == 'z' and i >= 0):
        i -= 1
 
    # If all characters are 'z', append
    # an 'a' at the end.
    if (i == -1):
        s = s + 'a'
 
    # If there are some non-z characters
    else:
        s = s.replace(s[i], chr(ord(s[i]) + 1), 1)
 
    return s
 
# Driver code
if __name__ == '__main__':
    str = "samez"
    print(nextWord(str))
     
# This code is contributed by
# Sanjit_Prasad

C#




// C# program to find
// lexicographically next string
using System;
 
class GFG
{
public static string nextWord(string str)
{
 
    // if string is empty
    if (str == "")
    {
    return "a";
    }
 
    // Find first character from
    // right which is not z.
    int i = str.Length - 1;
    while (str[i] == 'z' && i >= 0)
    {
        i--;
    }
 
    // If all characters are 'z',
    // append an 'a' at the end.
    if (i == -1)
    {
        str = str + 'a';
    }
 
    // If there are some
    // non-z characters
    else
    {
        str = str.Substring(0, i) +
             (char)((int)(str[i]) + 1) +
              str.Substring(i + 1);
    }
    return str;
}
 
// Driver Code
public static void Main(string[] args)
{
    string str = "samez";
    Console.Write(nextWord(str));
}
}
 
// This code is contributed by Shrikant13

Javascript




<script>
 
// Javascript program to find lexicographically next
// string
 
function nextWord(s)
{
    // If string is empty.
    if (s == "")
        return "a";
 
    // Find first character from right
    // which is not z.
     
    var i = s.length - 1;
    while (s[i] == 'z' && i >= 0)
        i--;
 
    // If all characters are 'z', append
    // an 'a' at the end.
    if (i == -1)
        s = s + 'a';
 
    // If there are some non-z characters
    else
        s[i] = String.fromCharCode(s[i].charCodeAt(0)+1);
 
    return s.join('');
}
 
// Driver code
var str = "samez".split('');
document.write( nextWord(str));
 
// This code is contributed by noob2000.
</script>

Output: 
 

samfz

This article is contributed by Pawan Asipu. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :