Sort a string lexicographically by reversing a substring
Given a string S consisting of N lowercase characters, the task is to find the starting and the ending indices ( 0-based indexing ) of the substring of the given string S that needed to be reversed to make the string S sorted. If it is not possible to sort the given string S by reversing any substring, then print “-1”.
Examples:
Input: S = “abcyxuz”
Output: 3 5
Explanation: Reversing the substring from indices [3, 5] modifies the string to “abcuxyz”, which is sorted.
Therefore, print 3 and 5.
Input: S = “GFG”
Output: 0 1
Naive Approach: The simplest approach to solve the given problem is to generate all possible substring of the given string S and if there exists any substring such reversing it makes the string sorted, then print the indices of that substrings. Otherwise, print “-1”.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized based on the observation that to sort the string by only reversing one substring, the original string must be in one of the following formats:
- Decreasing string
- Increasing substring + Decreasing substring
- Decreasing substring + Increasing substring
- Increasing substring + Decreasing substring + Increasing substring
Follow the steps below to solve the problem:
- Initialize two variables, say start and end as -1, that stores the starting and ending indices of the substring to be reversed respectively.
- Initialize a variable, say flag as 1, that stores if it is possible to sort the string or not.
- Iterate over the range [1, N] and perform the following operations:
- If the characters S[i] is less than characters S[i – 1] then find the index of the right boundary of the decreasing substring starting from the index (i – 1) and store it in end.
- Check if reversing the substring S[i – 1, end] makes the string sorted or not. If found to be false then print “-1” and return. Otherwise, mark the flag as false.
- After completing the above steps update the value of i with the right boundary of the substring.
- If the characters S[i] is less than characters S[i – 1] and the flag is false, then print “-1” and return.
- If the start is equal to -1, then update the value of start and end as 1.
- After completing the above steps, print the value of start and end as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool adjust(string& S, int & i,
int & start, int & end)
{
int N = S.length();
start = i - 1;
while (i < N && S[i] < S[i - 1]) {
i++;
}
end = i - 1;
if (start <= 0 && i >= N)
return true ;
if (start >= 1 && i <= N) {
return (S[end] >= S[start - 1]
&& S[start] <= S[i]);
}
if (start >= 1) {
return S[end] >= S[start - 1];
}
if (i < N) {
return S[start] <= S[i];
}
return false ;
}
void isPossible(string& S, int N)
{
int start = -1, end = -1;
bool flag = true ;
for ( int i = 1; i < N; i++) {
if (S[i] < S[i - 1]) {
if (flag) {
if (adjust(S, i, start, end)
== false ) {
cout << -1 << endl;
return ;
}
flag = false ;
}
else {
cout << -1 << endl;
return ;
}
}
}
if (start == -1) {
start = end = 1;
}
cout << start << " "
<< end << "\n" ;
}
int main()
{
string S = "abcyxuz" ;
int N = S.length();
isPossible(S, N);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG{
static int i, start, end;
static boolean adjust(String S)
{
int N = S.length();
start = i - 1 ;
while (i < N && S.charAt(i) <
S.charAt(i - 1 ))
{
i++;
}
end = i - 1 ;
if (start <= 0 && i >= N)
return true ;
if (start >= 1 && i <= N)
{
return (S.charAt(end) >= S.charAt(start - 1 ) &&
S.charAt(start) <= S.charAt(i));
}
if (start >= 1 )
{
return S.charAt(end) >= S.charAt(start - 1 );
}
if (i < N)
{
return S.charAt(start) <= S.charAt(i);
}
return false ;
}
static void isPossible(String S, int N)
{
start = - 1 ; end = - 1 ;
boolean flag = true ;
for (i = 1 ; i < N; i++)
{
if (S.charAt(i) < S.charAt(i - 1 ))
{
if (flag)
{
if (adjust(S) == false )
{
System.out.println(- 1 );
return ;
}
flag = false ;
}
else
{
System.out.println(- 1 );
return ;
}
}
}
if (start == - 1 )
{
start = end = 1 ;
}
System.out.println(start + " " + end);
}
public static void main(String[] args)
{
String S = "abcyxuz" ;
int N = S.length();
isPossible(S, N);
}
}
|
Python3
def adjust(S, i, start, end):
N = len (S)
start = i - 1
while (i < N and S[i] < S[i - 1 ]):
i + = 1
end = i - 1
if (start < = 0 and i > = N):
return True ,start,i,end
if (start > = 1 and i < = N):
return (S[end] > = S[start - 1 ] and S[start] < = S[i]),start,i,end
if (start > = 1 ):
return (S[end] > = S[start - 1 ]),start,i,end
if (i < N):
return (S[start] < = S[i]),start,i,end
return False ,start,i,end
def isPossible(S, N):
start, end = - 1 , - 1
flag = True
i = 1
while i < N:
if (S[i] < S[i - 1 ]):
if (flag):
f, start, i, end = adjust(S, i, start, end)
if (f = = False ):
print ( - 1 )
return
flag = False
else :
print ( - 1 )
return
i + = 1
if (start = = - 1 ):
start, end = 1 , 1
print (start, end)
if __name__ = = '__main__' :
S = "abcyxuz"
N = len (S)
isPossible(S, N)
|
C#
using System;
class GFG{
static int i, start, end;
static bool adjust( string S)
{
int N = S.Length;
start = i - 1;
while (i < N && S[i] < S[i - 1])
{
i++;
}
end = i - 1;
if (start <= 0 && i >= N)
return true ;
if (start >= 1 && i <= N)
{
return (S[end] >= S[start - 1] &&
S[start] <= S[i]);
}
if (start >= 1)
{
return S[end] >= S[start - 1];
}
if (i < N)
{
return S[start] <= S[i];
}
return false ;
}
static void isPossible( string S, int N)
{
start = -1; end = -1;
bool flag = true ;
for (i = 1; i < N; i++)
{
if (S[i] < S[i - 1])
{
if (flag)
{
if (adjust(S) == false )
{
Console.WriteLine(-1);
return ;
}
flag = false ;
}
else
{
Console.WriteLine(-1);
return ;
}
}
}
if (start == -1)
{
start = end = 1;
}
Console.WriteLine(start + " " + end);
}
static void Main()
{
string S = "abcyxuz" ;
int N = S.Length;
isPossible(S, N);
}
}
|
Javascript
<script>
var i, start, end;
function adjust(S) {
var N = S.length;
start = i - 1;
while (i < N && S[i] < S[i - 1]) {
i++;
}
end = i - 1;
if (start <= 0 && i >= N) return true ;
if (start >= 1 && i <= N) {
return S[end] >= S[start - 1] && S[start] <= S[i];
}
if (start >= 1) {
return S[end] >= S[start - 1];
}
if (i < N) {
return S[start] <= S[i];
}
return false ;
}
function isPossible(S, N) {
start = -1;
end = -1;
var flag = true ;
for (i = 1; i < N; i++) {
if (S[i] < S[i - 1]) {
if (flag) {
if (adjust(S) === false ) {
document.write(-1);
return ;
}
flag = false ;
}
else {
document.write(-1);
return ;
}
}
}
if (start === -1) {
start = end = 1;
}
document.write(start + " " + end + "<br>" );
}
var S = "abcyxuz" ;
var N = S.length;
isPossible(S, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
10 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...