Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

minimum number of cycle shifts for each string if it can be made palindrome

import java.util.Scanner;

public class PalyndromeTest {
    static boolean isPalyndrome(String s, int shift) {
        int n = s.length();
        if(shift < 0) shift+=n;
        for(int pos = 0; pos < n/2; pos++) {
            if(s.charAt((pos+shift)%n) != s.charAt((n-pos-1+shift)%n))
                return false;
        }
        return true;
    }

    static int findShift(String s) {
        for(int shift = 0; shift <= s.length()/2; shift++) {
            if(isPalyndrome(s, shift) || isPalyndrome(s, -shift))
                return shift;
        }
        return -1;
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int count = s.nextInt();
        s.nextLine();
        for(int i=0; i<count; i++) {
            System.out.println(findShift(s.nextLine()));
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: Vue In Typescript 
Typescript :: how to compile in typescript 
Typescript :: splice array based on index typescript 
Typescript :: What are the tables in test plans? 
Typescript :: pull rewuests in local project 
Typescript :: typescript import variable from another file 
Typescript :: c# check type implements generic interface 
Typescript :: typescript enum includes value 
Typescript :: nest custom class validator 
Typescript :: styled components tw 
Typescript :: typescript where to put interfaces 
Typescript :: print diagonal elements of matrix in c 
Typescript :: rest api django return value if exists in another table 
Typescript :: how to setup netflix workflow worker 
Cpp :: hello world c++ 
Cpp :: print set c++ 
Cpp :: select one random element of a vector in c++ 
Cpp :: remove or erase first and last character of string c++ 
Cpp :: npm install error 
Cpp :: c++ count bits 
Cpp :: 2114. Maximum Number of Words Found in Sentences leetcode solution in c++ 
Cpp :: should i learn c or c++ 
Cpp :: uri online judge 3145 solution in c++ 
Cpp :: what are specialized classes c++ 
Cpp :: c++ save typeid 
Cpp :: average of a matrix c++ 
Cpp :: add on screen debug message ue4 
Cpp :: ifstream relative file path 
Cpp :: c++ random number generator uniform distribution 
Cpp :: c++ round number to whole 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =