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 and str[n – 1] are adjacent to each other.
Input: str = “SSNSS”, i = 0, j = 3
From left to right : S -> S -> N -> S
From right to left : S -> S -> S
Input: str = “geeksforgeeks”, i = 0, j = 3
- 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 != str[n – 1].
- Print min(step1, step2) in the end.
Below is the implementation of the above approach:
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.
- Minimal moves to form a string by adding characters or appending string itself
- Count of cyclic permutations having XOR with other binary string as 0
- Count number of ways to convert string S to T by performing K cyclic shifts
- Sort a string lexicographically using triple cyclic shifts
- Find the minimum number of preprocess moves required to make two strings equal
- Minimum moves required to type a word in QWERTY based keyboard
- Find minimum number of steps to reach the end of String
- Count all indices of cyclic regular parenthesis
- Minimum bit changes in Binary Circular array to reach a index
- Check if a given sequence of moves for a robot is circular or not
- Print direction of moves such that you stay within the [-k, +k] boundary
- Maximize Sum possible from an Array by the given moves
- Maximum power of jump required to reach the end of string
- Find time taken for signal to reach all positions in a string
- Minimize steps defined by a string required to reach the destination from a given source
- Print all possible shortest chains to reach a target word
- Find the character in first string that is present at minimum index in second string
- Convert string X to an anagram of string Y with minimum replacements
- Minimum length of string having all permutation of given string.
- Minimum changes required to make first string substring of second string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.