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:
- Count of cyclic permutations having XOR with other binary string as 0
- Minimal moves to form a string by adding characters or appending string itself
- Find the minimum number of preprocess moves required to make two strings equal
- Minimum bit changes in Binary Circular array to reach a index
- Maximum power of jump required to reach the end of string
- Find time taken for signal to reach all positions in a string
- Find the character in first string that is present at minimum index in second string
- Minimum number of given operations required to convert a string to another string
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Minimum changes required to make first string substring of second string
- Covert string X to an anagram of string Y with minimum replacements
- Minimum length of string having all permutation of given string.
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum rotations required to get the same string
- Minimum Distance Between Words of a 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 firstname.lastname@example.org. 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.