String character swap problem
Last Updated :
15 Sep, 2023
Given a string S of length N consisting of characters ‘L‘ (sees every character lying to its left) or ‘R‘ (sees every character lying to its right). In one operation, any two consecutive characters can be swapped. The operation can be performed at most once. Determine whether or not each character is being seen by at least one character. Print -1 if it is impossible, 0 if no swap is needed, or the index (1 – indexed) of the first friend if a swap is needed. Note: A character cannot see itself.
Examples:
Input: N = 2, S = “LL”
Output: -1
Explanation: It is impossible even after swapping the two.
Input: N = 2, S = “LR”
Output: 1
Explanation: It is necessary to swap friends 1 and 2. The string becomes “RL”. Friend 1 is looking at friend 2 and friend 2 is looking at friend 1.
Input: N = 2, S = “RL”
Output: 0
Explanation: Every friend is already being seen. No swap is needed.
Approach: This can be solved with the following idea:
- If there is an index i such that si = R and si+1 = L, then friend i will see friends i+1, i+2, …, n, and friend i+1 will see 1, 2, …, i. In this case, if L appears right after some R, then all friends are already seen. Therefore, strings like LLRRLL, LRLRLR, RRRLLL, etc. do not require any further operations.
- However, if there is no such index i, we need to perform an operation to see all the friends. We can do this by transforming LR to RL if it appears in the string since we have already concluded that all friends are seen in that case.
- An edge case arises when L and R are never adjacent (neither LR nor RL appears). In this case, si = si+1 must hold for i=1, 2, …, n−1, which means that LL…L and RR…R are the only impossible strings for which the answer is -1.
Steps involved in the implementation of code:
- Initialize Boolean variables ‘left‘, ‘right‘, and ‘check‘ to false.
- Loop through the characters of the string ‘s‘ from ‘left’ to ‘right’. For each character, if it is ‘L’, set ‘left’ to true, set ‘ind‘ to the index of the character plus one, and if ‘right‘ is also true, set ‘check‘ to true. If the character is ‘R‘, set right to true.
- After looping through the string ‘s’, check the value of ‘check‘. If it is true, output 0 and exit the function.
- Otherwise output ‘-1‘ and exit the function.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
using namespace std;
void left_right(string s, int n)
{
bool left = 0, right = 0, check = 0;
int ind = -1;
for ( int i = 0; i < n; i++) {
if (s[i] == 'L' ) {
left = 1;
ind = i + 1;
if (right)
check = 1;
}
else
right = 1;
}
if (check)
cout << 0 << endl;
else
cout << ind << endl;
}
int main()
{
int n = 5;
string s = "RRLRL" ;
left_right(s, n);
return 0;
}
|
Java
public class GFG {
static void left_right(String s, int n) {
boolean left = false , right = false , check = false ;
int ind = - 1 ;
for ( int i = 0 ; i < n; i++) {
if (s.charAt(i) == 'L' ) {
left = true ;
ind = i + 1 ;
if (right)
check = true ;
}
else
right = true ;
}
if (check)
System.out.println( 0 );
else
System.out.println(ind);
}
public static void main(String[] args) {
int n = 5 ;
String s = "RRLRL" ;
left_right(s, n);
}
}
|
Python3
def left_right(s, n):
left = False
right = False
check = False
ind = - 1
for i in range (n):
if s[i] = = 'L' :
left = True
ind = i + 1
if right:
check = True
else :
right = True
if check:
print ( 0 )
else :
print (ind)
n = 5
s = "RRLRL"
left_right(s, n)
|
C#
using System;
public class GFG
{
static void left_right( string s, int n)
{
bool right = false , check = false ;
int ind = -1;
for ( int i = 0; i < n; i++)
{
if (s[i] == 'L' )
{
ind = i + 1;
if (right)
check = true ;
}
else
right = true ;
}
if (check)
Console.WriteLine(0);
else
Console.WriteLine(ind);
}
public static void Main( string [] args)
{
int n = 5;
string s = "RRLRL" ;
left_right(s, n);
}
}
|
Javascript
function left_right(s, n) {
let left = false ;
let right = false ;
let check = false ;
let ind = -1;
for (let i = 0; i < n; i++) {
if (s[i] === 'L' ) {
left = true ;
ind = i + 1;
if (right) {
check = true ;
}
} else {
right = true ;
}
}
if (check) {
console.log(0);
} else {
console.log(ind);
}
}
const n = 5;
const s = "RRLRL" ;
left_right(s, n);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...