Encuentre una string en orden lexicográfico que esté entre dos strings dadas

Dadas dos strings S y T, encuentre una string de la misma longitud que sea lexicográficamente mayor que S y más pequeña que T. Escriba “-1” si no se forma dicha string. (S > T) 

Nota: se dice que la string S = s1s2… sn es lexicográficamente más pequeña que la string T = t1t2… tn, si existe una i, tal que s1 = t1, s2 = t2, … si – 1 = ti – 1, si < ti .

C++

// CPP program to find the string
// in lexicographic order which is
// in between given two strings
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the lexicographically 
// next string
string lexNext(string s, int n)
{  
    // Iterate from last character
    for (int i = n - 1; i >= 0; i--)
    {  
        // If not 'z', increase by one
        if (s[i] != 'z')
        {
            s[i]++;
            return s;
        }
         
        // if 'z', change it to 'a'
        s[i] = 'a';
    }
}
 
// Driver Code
int main()
{
    string S = "abcdeg", T = "abcfgh";
    int n = S.length();
    string res = lexNext(S, n);
 
    // If not equal, print the
    // resultant string
    if (res != T)
        cout << res << endl;   
    else
        cout << "-1" << endl;
    return 0;
}

Java

//Java program to find the string
// in lexicographic order which is
// in between given two strings
 
class GFG {
 
// Function to find the lexicographically 
// next string
    static String lexNext(String str, int n) {
        char[] s = str.toCharArray();
        // Iterate from last character
        for (int i = n - 1; i >= 0; i--) {
            // If not 'z', increase by one
            if (s[i] != 'z') {
                s[i]++;
                return String.valueOf(s);
            }
 
            // if 'z', change it to 'a'
            s[i] = 'a';
        }
        return null;
    }
 
// Driver Code
    static public void main(String[] args) {
        String S = "abcdeg", T = "abcfgh";
        int n = S.length();
        String res = lexNext(S, n);
 
        // If not equal, print the
        // resultant string
        if (res != T) {
            System.out.println(res);
        } else {
            System.out.println("-1");
        }
    }
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 program to find the string
# in lexicographic order which is
# in between given two strings
 
# Function to find the lexicographically
# next string
def lexNext(s, n):
 
    # Iterate from last character
    for i in range(n - 1, -1, -1):
 
        # If not 'z', increase by one
        if s[i] != 'z':
            k = ord(s[i])
            s[i] = chr(k + 1)
            return ''.join(s)
 
        # if 'z', change it to 'a'
        s[i] = 'a'
 
# Driver Code
if __name__ == "__main__":
    S = "abcdeg"
    T = "abcfgh"
    n = len(S)
 
    S = list(S)
    res = lexNext(S, n)
 
    # If not equal, print the
    # resultant string
    if res != T:
        print(res)
    else:
        print(-1)
 
# This code is contributed by
# sanjeev2552

C#

//C# program to find the string
// in lexicographic order which is
// in between given two strings
using System;
 
public class GFG {
  
// Function to find the lexicographically 
// next string
    static String lexNext(String str, int n) {
        char[] s = str.ToCharArray();
        // Iterate from last character
        for (int i = n - 1; i >= 0; i--) {
            // If not 'z', increase by one
            if (s[i] != 'z') {
                s[i]++;
                return new String(s);
            }
  
            // if 'z', change it to 'a'
            s[i] = 'a';
        }
        return null;
    }
  
// Driver Code
    static public void Main() {
        String S = "abcdeg", T = "abcfgh";
        int n = S.Length;
        String res = lexNext(S, n);
  
        // If not equal, print the
        // resultant string
        if (res != T) {
            Console.Write(res);
        } else {
            Console.Write("-1");
        }
    }
}
  
// This code is contributed by 29AjayKumar

Javascript

<script>
 
// JavaScript program to find the string
// in lexicographic order which is
// in between given two strings
 
// Function to find the lexicographically 
// next string
function lexNext( s, n){  
    // Iterate from last character
    for (let i = n - 1; i >= 0; i--)
    {  
        // If not 'z', increase by one
        if (s[i] != 'z')
        {
            let code = s.charCodeAt(i)+1;
            let str = String.fromCharCode(code);
            return s.substr(0,i)+str+s.substr(i+1);
 
        }
         
        // if 'z', change it to 'a'
        s[i] = 'a';
    }
     
}
 
// Driver Code
let S = "abcdeg";
let T = "abcfgh";
let n = S.length;
let res = lexNext(S, n);
 
// If not equal, print the
// resultant string
if (res != T)
   document.write( res,'<br>'); 
    else
     document.write("-1 <br>" );
      
</script>

Publicación traducida automáticamente

Artículo escrito por Harsha_Mogali y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *