Number of Counterclockwise shifts to make a string palindrome

Given a string of lowercase English alphabets, find the number of counterclockwise shifts of characters required to make the string palindrome. It is given that shifting the string will always result in the palindrome.

Examples:

Input: str = “baabbccb”
Output: 2
Shifting the string counter clockwise 2 times,
will make the string palindrome.
1st shift : aabbccbb
2nd shift :abbccbba

Input: bbaabbcc
Output: 3
Shifting the string counter clockwise
3 times will make the string palindrome.
1st shift : baabbccb
2nd shift : aabbccbb
3rd shift : abbccbba



Naive Approach: A naive approach is to one by one shift character of the given string counter-clockwise cyclically and check if the string is palindrome or not.

Better Approach: A better approach is to append the string with itself and iterate from the first character to the last character of the given string. The substring from i to i+n (where i is in range [0,n-1]) in the appended string will be the string obtained after every counterclockwise shift. Check for the substring if it is palindrome or not. The number of shift operations will be i.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find counter clockwise
// shifts to make string palindrome.
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if given string is
// palindrome or not.
bool isPalindrome(string str, int l, int r)
{
    while (l < r) {
        if (str[l] != str[r])
            return false;
  
        l++;
        r--;
    }
  
    return true;
}
  
// Function to find counter clockwise shifts
// to make string palindrome.
int CyclicShifts(string str)
{
  
    int n = str.length();
  
    // Pointer to starting of current
    // shifted string.
    int left = 0;
  
    // Pointer to ending of current
    // shifted string.
    int right = n - 1;
  
    // Concatenate string with itself
    str = str + str;
  
    // To store counterclockwise shifts
    int cnt = 0;
  
    // Move left and right pointers one
    // step at a time.
    while (right < 2 * n - 1) {
  
        // Check if current shifted string
        // is palindrome or not
        if (isPalindrome(str, left, right))
            break;
  
        // If string is not palindrome
        // then increase count of number
        // of shifts by 1.
        cnt++;
  
        left++;
        right++;
    }
  
    return cnt;
}
  
// Driver code.
int main()
{
    string str = "bccbbaab";
  
    cout << CyclicShifts(str);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find counter clockwise
// shifts to make string palindrome.
class GFG 
{
  
// Function to check if given string is
// palindrome or not.
static boolean isPalindrome(String str, int l, int r)
{
    while (l < r) 
    {
        if (str.charAt(l) != str.charAt(r))
            return false;
  
        l++;
        r--;
    }
    return true;
}
  
// Function to find counter clockwise shifts
// to make string palindrome.
static int CyclicShifts(String str)
{
  
    int n = str.length();
  
    // Pointer to starting of current
    // shifted string.
    int left = 0;
  
    // Pointer to ending of current
    // shifted string.
    int right = n - 1;
  
    // Concatenate string with itself
    str = str + str;
  
    // To store counterclockwise shifts
    int cnt = 0;
  
    // Move left and right pointers one
    // step at a time.
    while (right < 2 * n - 1
    {
  
        // Check if current shifted string
        // is palindrome or not
        if (isPalindrome(str, left, right))
            break;
  
        // If string is not palindrome
        // then increase count of number
        // of shifts by 1.
        cnt++;
  
        left++;
        right++;
    }
    return cnt;
}
  
// Driver code.
public static void main(String[] args) 
{
    String str = "bccbbaab";
  
    System.out.println(CyclicShifts(str));
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find counter clockwise
# shifts to make string palindrome.
  
# Function to check if given string 
# is palindrome or not.
def isPalindrome(str, l, r):
  
    while (l < r) :
        if (str[l] != str[r]):
            return False
  
        l += 1
        r -= 1
  
    return True
  
# Function to find counter clockwise 
# shifts to make string palindrome.
def CyclicShifts(str):
  
    n = len(str)
  
    # Pointer to starting of current
    # shifted string.
    left = 0
  
    # Pointer to ending of current
    # shifted string.
    right = n - 1
  
    # Concatenate string with itself
    str = str + str
  
    # To store counterclockwise shifts
    cnt = 0
  
    # Move left and right pointers 
    # one step at a time.
    while (right < 2 * n - 1) :
  
        # Check if current shifted string
        # is palindrome or not
        if (isPalindrome(str, left, right)):
            break
  
        # If string is not palindrome
        # then increase count of number
        # of shifts by 1.
        cnt += 1
  
        left += 1
        right += 1
  
    return cnt
  
# Driver code.
if __name__ == "__main__":
      
    str = "bccbbaab";
  
    print(CyclicShifts(str))
  
# This code is contributed by ita_c

chevron_right