Your browser (Unknown 0) is no longer supported. Some parts of the website may not work correctly. Please update your browser.
Find a symmetry point of a string, if any.
Spoken language:

Write a function:

class Solution { public int solution(String S); }

that, given a string S, returns the index (counting from 0) of a character such that the part of the string to the left of that character is a reversal of the part of the string to its right. The function should return −1 if no such index exists.

Note: reversing an empty string (i.e. a string whose length is zero) gives an empty string.

For example, given a string:


the function should return 3, because the substring to the left of the character "e" at index 3 is "rac", and the one to the right is "car".

Given a string:


the function should return 0, because both substrings are empty.

Write an efficient algorithm for the following assumptions:

  • the length of S is within the range [0..2,000,000].
Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.