Find the direction from given string
Last Updated :
05 Sep, 2022
Given a string containing only L’s and R’s which represents left rotation and right rotation respectively. The task is to find the final direction of pivot(i.e N/E/S/W). Let a pivot is pointed towards the north(N) in a compass.
Examples:
Input: str = "LLRLRRL"
Output: W
In this input string we rotate pivot to left
when a L char is encountered and right when
R is encountered.
Input: str = "LL"
Output: S
Approach:
- Use a counter that incremented on seeing R and decremented on seeing L.
- Finally, use modulo on the counter to get the direction.
- If the count is negative then directions will be different. Check the code for negative as well.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string findDirection(string s)
{
int count = 0;
string d = "" ;
for ( int i = 0; i < s.length(); i++) {
if (s[0] == '\n' )
return NULL;
if (s[i] == 'L' )
count--;
else {
if (s[i] == 'R' )
count++;
}
}
if (count > 0) {
if (count % 4 == 0)
d = "N" ;
else if (count % 4 == 1)
d = "E" ;
else if (count % 4 == 2)
d = "S" ;
else if (count % 4 == 3)
d = "W" ;
}
if (count < 0) {
if (count % 4 == 0)
d = "N" ;
else if (count % 4 == -1)
d = "W" ;
else if (count % 4 == -2)
d = "S" ;
else if (count % 4 == -3)
d = "E" ;
}
return d;
}
int main()
{
string s = "LLRLRRL" ;
cout << (findDirection(s)) << endl;
s = "LL" ;
cout << (findDirection(s)) << endl;
}
|
Java
import java.util.*;
class GFG {
static String findDirection(String s)
{
int count = 0 ;
String d = "" ;
for ( int i = 0 ; i < s.length(); i++) {
if (s.charAt( 0 ) == '\n' )
return null ;
if (s.charAt(i) == 'L' )
count--;
else {
if (s.charAt(i) == 'R' )
count++;
}
}
if (count > 0 ) {
if (count % 4 == 0 )
d = "N" ;
else if (count % 4 == 1 )
d = "E" ;
else if (count % 4 == 2 )
d = "S" ;
else if (count % 4 == 3 )
d = "W" ;
}
if (count < 0 ) {
if (count % 4 == 0 )
d = "N" ;
else if (count % 4 == - 1 )
d = "W" ;
else if (count % 4 == - 2 )
d = "S" ;
else if (count % 4 == - 3 )
d = "E" ;
}
return d;
}
public static void main(String[] args)
{
String s = "LLRLRRL" ;
System.out.println(findDirection(s));
s = "LL" ;
System.out.println(findDirection(s));
}
}
|
Python3
def findDirection(s):
count = 0
d = ""
for i in range ( len (s)):
if (s[i] = = 'L' ):
count - = 1
else :
if (s[i] = = 'R' ):
count + = 1
if (count > 0 ):
if (count % 4 = = 0 ):
d = "N"
elif (count % 4 = = 1 ):
d = "E"
elif (count % 4 = = 2 ):
d = "S"
elif (count % 4 = = 3 ):
d = "W"
if (count < 0 ):
count * = - 1
if (count % 4 = = 0 ):
d = "N"
elif (count % 4 = = 1 ):
d = "W"
elif (count % 4 = = 2 ):
d = "S"
elif (count % 4 = = 3 ):
d = "E"
return d
if __name__ = = '__main__' :
s = "LLRLRRL"
print (findDirection(s))
s = "LL"
print (findDirection(s))
|
C#
using System;
class GFG {
static String findDirection(String s)
{
int count = 0;
String d = "" ;
for ( int i = 0; i < s.Length; i++) {
if (s[0] == '\n' )
return null ;
if (s[i] == 'L' )
count--;
else {
if (s[i] == 'R' )
count++;
}
}
if (count > 0) {
if (count % 4 == 0)
d = "N" ;
else if (count % 4 == 1)
d = "E" ;
else if (count % 4 == 2)
d = "S" ;
else if (count % 4 == 3)
d = "W" ;
}
if (count < 0) {
if (count % 4 == 0)
d = "N" ;
else if (count % 4 == -1)
d = "W" ;
else if (count % 4 == -2)
d = "S" ;
else if (count % 4 == -3)
d = "E" ;
}
return d;
}
public static void Main()
{
String s = "LLRLRRL" ;
Console.WriteLine(findDirection(s));
s = "LL" ;
Console.WriteLine(findDirection(s));
}
}
|
PHP
<?php
function findDirection( $s )
{
$count = 0;
$d = "" ;
for ( $i = 0;
$i < strlen ( $s ); $i ++)
{
if ( $s [0] == '\n' )
return null;
if ( $s [ $i ] == 'L' )
$count -= 1;
else
{
if ( $s [ $i ] == 'R' )
$count += 1;
}
}
if ( $count > 0)
{
if ( $count % 4 == 0)
$d = "N" ;
else if ( $count % 4 == 1)
$d = "E" ;
else if ( $count % 4 == 2)
$d = "S" ;
else if ( $count % 4 == 3)
$d = "W" ;
}
if ( $count < 0)
{
if ( $count % 4 == 0)
$d = "N" ;
else if ( $count % 4 == -1)
$d = "W" ;
else if ( $count % 4 == -2)
$d = "S" ;
else if ( $count % 4 == -3)
$d = "E" ;
}
return $d ;
}
$s = "LLRLRRL" ;
echo findDirection( $s ). "\n" ;
$s = "LL" ;
echo findDirection( $s ). "\n" ;
?>
|
Javascript
<script>
function findDirection(s)
{
let count = 0;
let d = "" ;
for (let i = 0; i < s.length; i++) {
if (s[0] == '\n' )
return null ;
if (s[i] == 'L' )
count--;
else {
if (s[i] == 'R' )
count++;
}
}
if (count > 0) {
if (count % 4 == 0)
d = "N" ;
else if (count % 4 == 1)
d = "E" ;
else if (count % 4 == 2)
d = "S" ;
else if (count % 4 == 3)
d = "W" ;
}
if (count < 0) {
if (count % 4 == 0)
d = "N" ;
else if (count % 4 == -1)
d = "W" ;
else if (count % 4 == -2)
d = "S" ;
else if (count % 4 == -3)
d = "E" ;
}
return d;
}
let s = "LLRLRRL" ;
document.write(findDirection(s) + "</br>" );
s = "LL" ;
document.write(findDirection(s));
</script>
|
Complexity Analysis:
- Time Complexity: O(N) where N is the length of the string
- Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...