📌  相关文章
📜  按字典顺序排列的中间字符串

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

按字典顺序排列的中间字符串

给定两个字符串ab 。我们的任务是打印任何大于a (按字典顺序)但小于b (按字典顺序)的字符串。如果不可能得到这样的字符串,打印-1;

例子:

Input : a = "abg", b = "abj"
Output : abh
The string "abh" is lexicographically 
greater than "abg" and smaller than
"abj"        

Input : a = "abc", b = "abd"
Output :-1
There is no string which is lexicographically
greater than a but smaller than b/

由于可能有多个字符串可以满足上述条件,我们将字符串“a”转换为按字典顺序排列在“a”旁边的字符串。

接下来要按字典顺序查找,我们开始从后向遍历字符串并将所有字母“z”转换为字母“a”。如果遇到任何不是“z”的字母,则将其加一,不再进行遍历。如果该字符串不小于“b”,则打印-1,因为没有字符串可以满足以上条件。

例如,字符串a=”ddzzz” 和字符串b=”deaao”。所以,从后向开始,我们会将所有字母“z”转换为字母“a”,直到我们到达字母“d”(在这种情况下)。将“d”加一(到“e”)并从循环中跳出。因此,字符串a将变为“deaaa”,按字典顺序大于“ddzzz”且小于“deaao”。

C++
// C++ program to implement above approach
#include 
using namespace std;
 
// function to find lexicographically mid
// string.
void lexMiddle(string a, string b)
{
    // converting string "a" into its
    // lexicographically next string
    for (int i = a.length() - 1; i >= 0; i--) {
 
        // converting all letter "z" to letter "a"
        if (a[i] == 'z')
            a[i] = 'a';
        else {
 
            // if letter other than "z" is
            // encountered, increment it by one
            // and break
            a[i]++;
            break;
        }
    }
 
    // if this new string "a" is lexicographically
    // smaller than b
    if (a < b)
        cout << a;
    else
        cout << -1;
}
 
// Driver function
int main()
{
    string a = "geeks", b = "heeks";
    lexMiddle(a, b);
    return 0;
}


Java
// Java program to implement
// above approach
class GFG
{
 
// function to find lexicographically
// mid String.
static void lexMiddle(String a, String b)
{
    String new_String = "";
     
    // converting String "a" into its
    // lexicographically next String
    for (int i = a.length() - 1; i >= 0; i--)
    {
 
        // converting all letter
        // "z" to letter "a"
        if (a.charAt(i) == 'z')
            new_String = 'a' + new_String;
        else
        {
             
            // if letter other than "z" is
            // encountered, increment it by
            // one and break
            new_String = (char)(a.charAt(i) + 1) +
                                       new_String;
             
            //compose the remaining string
            for(int j = i - 1; j >= 0; j--)
            new_String = a.charAt(j) + new_String;
             
            break;
        }
    }
 
    // if this new String new_String is
    // lexicographically smaller than b
    if (new_String.compareTo(b) < 0)
    System.out.println(new_String);
    else
        System.out.println(-1);
}
 
// Driver Code
public static void main(String args[])
{
    String a = "geeks", b = "heeks";
    lexMiddle(a, b);
}
}
 
// This code is contributed
// by Arnab Kundu


Python3
# Python3 program to implement above approach
 
# function to find lexicographically mid
# string.
def lexMiddle( a, b):
 
    # converting string "a" into its
    # lexicographically next string
    for i in range(len(a)-1,-1,-1):
     
        ans=[]
        # converting all letter "z" to letter "a"
        if (a[i] == 'z'):
            a[i] = 'a'
        else:
 
            # if letter other than "z" is
            # encountered, increment it by one
            # and break
            a[i]=chr(ord(a[i])+1)
            break
     
     
 
    # if this new string "a" is lexicographically
    # smaller than b
    if (a < b):
        return a
    else:
        return -1
 
 
 
# Driver function
if __name__=='__main__':
    a = list("geeks")
    b = list("heeks")
    ans=lexMiddle(a, b)
    ans = ''.join(map(str, ans))
    print(ans)
 
# this code is contributed by ash264


C#
// C# program to implement above approach
using System;
 
class GFG
{
 
// function to find lexicographically
// mid String.
static void lexMiddle(string a, string b)
{
    string new_String = "";
     
    // converting String "a" into its
    // lexicographically next String
    for (int i = a.Length - 1; i >= 0; i--)
    {
 
        // converting all letter
        // "z" to letter "a"
        if (a[i] == 'z')
            new_String = 'a' + new_String;
        else
        {
             
            // if letter other than "z" is
            // encountered, increment it by
            // one and break
            new_String = (char)(a[i] + 1) +
                                new_String;
             
            //compose the remaining string
            for(int j = i - 1; j >= 0; j--)
                new_String = a[j] + new_String;
             
            break;
        }
    }
 
    // if this new String new_String is
    // lexicographically smaller than b
    if (new_String.CompareTo(b) < 0)
    Console.Write(new_String);
    else
        Console.Write(-1);
}
 
// Driver Code
public static void Main()
{
    string a = "geeks", b = "heeks";
    lexMiddle(a, b);
}
}
 
// This code is contributed by ita_c


Javascript


输出:
geekt

时间复杂度: O(n),其中 n 是字符串'a' 的长度