📌  相关文章
📜  按字典顺序排列的下一个字符串

📅  最后修改于: 2022-05-13 01:57:06.609000             🧑  作者: Mango

按字典顺序排列的下一个字符串

给定一个字符串,按字典顺序查找下一个字符串。
例子:

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

如果字符串为空,我们返回'a'。如果字符串包含所有字符为“z”,我们在末尾附加“a”。否则,我们从 end 中找到第一个不是 z 的字符并将其递增。

C++
// C++ program to find lexicographically next
// string
#include 
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


输出:

samfz