# Lexicographically middle string

• Last Updated : 12 May, 2021

Given two strings a and b. Our task is to print any string which is greater than a(lexicographically) but smaller than b(lexicographically). If it is impossible to get such string, print -1;

Examples:

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.

```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/```

Since, there can be multiple strings which may satisfy the above condition, we convert string “a” into a string which is lexicographically next to “a”.

To find lexicographically next, we start traversing the string from backward and convert all the letter “z” to letter”a”. If we encounter any letter which is not “z”, then we increment it by one and further traversal will not be carried out.If this string is not smaller than “b”, then we will print -1 as no string can satisfy the above condition.

For example, string a=”ddzzz” and string b=”deaao”.So, starting from backward, we will convert all letter “z” to letter “a” until we reach to letter “d”(in this case).Increment “d” by one (to “e”) and break out from the loop.So, string a will become “deaaa” which is lexicographically greater than “ddzzz” and smaller than “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

 ``
Output:
`geekt`

Time Complexity: O(n) where n is length of string ‘a’

My Personal Notes arrow_drop_up