Given a cyclic string str and two integers i and j, the task is to count the minimum number of steps required to move from str[i] to str[j]. A move is to reach any adjacent character in the string and the move is only counted if str[start] != start[end] where start is the starting index for the move and end is the ending (adjacent either on the left or on the right) index. Since, the given string is circular, str[0] and str[n – 1] are adjacent to each other.
Examples:
Input: str = "SSNSS", i = 0, j = 3
Output: 0 From left to right : S -> S -> N -> S From right to left : S -> S -> S
Input: str = "geeksforgeeks", i = 0, j = 3
Output: 2
Approach:
- Starting from index i start moving in the right direction till index j and for every character visited, if the current character is not equal to the previous character then increment steps1 = steps1 + 1.
- Similarly, starting from i start moving in the left direction till index 0 and for every character visited, if the current character is not equal to the previous character then increment steps2 = steps2 + 1. Once the index 0 is visited, start traversing from index n – 1 to j and increment step2 if str[0] != str[n – 1].
- Print min(step1, step2) in the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getSteps(string str, int i, int j, int n)
{
int k = i + 1;
int steps = 0;
char ch = str[i];
while (k <= j) {
if (str[k] != ch) {
steps++;
ch = str[k];
}
k++;
}
return steps;
}
int getMinSteps(string str, int i, int j, int n)
{
if (j < i) {
int temp = i;
i = j;
j = temp;
}
int stepsToRight = getSteps(str, i, j, n);
int stepsToLeft = getSteps(str, 0, i, n)
+ getSteps(str, j, n - 1, n);
if (str[0] != str[n - 1])
stepsToLeft++;
return min(stepsToLeft, stepsToRight);
}
int main()
{
string str = "SSNSS" ;
int n = str.length();
int i = 0, j = 3;
cout << getMinSteps(str, i, j, n);
return 0;
}
|
Java
class GFG
{
static int getSteps(String str, int i, int j, int n)
{
int k = i + 1 ;
int steps = 0 ;
char ch = str.charAt(i);
while (k <= j)
{
if (str.charAt(k) != ch)
{
steps++;
ch = str.charAt(k);
}
k++;
}
return steps;
}
static int getMinSteps(String str, int i, int j, int n)
{
if (j < i)
{
int temp = i;
i = j;
j = temp;
}
int stepsToRight = getSteps(str, i, j, n);
int stepsToLeft = getSteps(str, 0 , i, n)
+ getSteps(str, j, n - 1 , n);
if (str.charAt( 0 ) != str.charAt(n - 1 ))
stepsToLeft++;
return Math.min(stepsToLeft, stepsToRight);
}
public static void main(String []args)
{
String str = "SSNSS" ;
int n = str.length();
int i = 0 , j = 3 ;
System.out.println(getMinSteps(str, i, j, n));
}
}
|
Python3
def getSteps( str , i, j, n) :
k = i + 1
steps = 0
ch = str [i]
while (k < = j):
if ( str [k] ! = ch):
steps = steps + 1
ch = str [k]
k = k + 1
return steps
def getMinSteps( str , i, j, n):
if (j < i):
temp = i
i = j
j = temp
stepsToRight = getSteps( str , i, j, n)
stepsToLeft = getSteps( str , 0 , i, n) + getSteps( str , j, n - 1 , n)
if ( str [ 0 ] ! = str [n - 1 ]):
stepsToLeft = stepsToLeft + 1
return min (stepsToLeft, stepsToRight)
str = "SSNSS"
n = len ( str )
i = 0
j = 3
print (getMinSteps( str , i, j, n))
|
C#
using System;
class GFG
{
static int getSteps( string str, int i, int j, int n)
{
int k = i + 1;
int steps = 0;
char ch = str[i];
while (k <= j)
{
if (str[k] != ch)
{
steps++;
ch = str[k];
}
k++;
}
return steps;
}
static int getMinSteps( string str, int i, int j, int n)
{
if (j < i)
{
int temp = i;
i = j;
j = temp;
}
int stepsToRight = getSteps(str, i, j, n);
int stepsToLeft = getSteps(str, 0, i, n)
+ getSteps(str, j, n - 1, n);
if (str[0] != str[n - 1])
stepsToLeft++;
return Math.Min(stepsToLeft, stepsToRight);
}
public static void Main()
{
string str = "SSNSS" ;
int n = str.Length;
int i = 0, j = 3;
Console.WriteLine(getMinSteps(str, i, j, n));
}
}
|
PHP
<?php
function getSteps( $str , $i , $j , $n )
{
$k = $i + 1;
$steps = 0;
$ch = $str [ $i ];
while ( $k <= $j )
{
if ( $str [ $k ] != $ch )
{
$steps ++;
$ch = $str [ $k ];
}
$k ++;
}
return $steps ;
}
function getMinSteps( $str , $i , $j , $n )
{
if ( $j < $i )
{
$temp = $i ;
$i = $j ;
$j = $temp ;
}
$stepsToRight = getSteps( $str , $i , $j , $n );
$stepsToLeft = getSteps( $str , 0, $i , $n ) +
getSteps( $str , $j , $n - 1, $n );
if ( $str [0] != $str [ $n - 1])
$stepsToLeft ++;
return min( $stepsToLeft , $stepsToRight );
}
$str = "SSNSS" ;
$n = strlen ( $str );
$i = 0;
$j = 3;
echo getMinSteps( $str , $i , $j , $n );
?>
|
Javascript
function getSteps(str, i, j, n)
{
let k = i + 1;
let steps = 0;
let ch = str[i];
while (k <= j) {
if (str[k] != ch) {
steps++;
ch = str[k];
}
k++;
}
return steps;
}
function getMinSteps(str, i, j, n)
{
if (j < i) {
let temp = i;
i = j;
j = temp;
}
let stepsToRight = getSteps(str, i, j, n);
let stepsToLeft = getSteps(str, 0, i, n)
+ getSteps(str, j, n - 1, n);
if (str[0] != str[n - 1])
stepsToLeft++;
return Math.min(stepsToLeft, stepsToRight);
}
let str = "SSNSS" ;
let n = str.length;
let i = 0;
let j = 3;
console.log(getMinSteps(str, i, j, n));
|
Complexity Analysis:
- Time Complexity: O(j)
- Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
09 Dec, 2022
Like Article
Save Article