Tasks Details
easy
Find a symmetry point of a string, if any.
Task Score
100%
Correctness
100%
Performance
100%
Write a function:
function solution(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:
"racecar"
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:
"x"
the function should return 0, because both substrings are empty.
Write an efficient algorithm for the following assumptions:
- the length of string S is within the range [0..2,000,000].
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used JavaScript
Time spent on task 24 minutes
Notes
not defined yet
Code: 12:36:08 UTC,
java,
autosave
Code: 12:38:30 UTC,
js,
autosave
Code: 12:40:33 UTC,
js,
autosave
Code: 12:40:52 UTC,
js,
autosave
Code: 12:41:22 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
let start = 0;
let end = S.length - 1;
while (start < end) {
}
}
function reverseString(S) {
let split = S.split('');
let reverseSplit = split.reverse()
}
Code: 12:41:39 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
let start = 0;
let end = S.length - 1;
while (start < end) {
}
}
function reverseString(S) {
let split = S.split('');
let reverseSplit = split.reverse();
return S.
}
Code: 12:41:50 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
let start = 0;
let end = S.length - 1;
while (start < end) {
}
}
function reverseString(S) {
let split = S.split('');
let reverseSplit = split.reverse();
return S.join('')
}
Code: 12:42:10 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
let start = 0;
let end = S.length - 1;
while (start < end) {
}
}
function reverseString(S) {
let split = S.split('').reverse();
let reverseSplit = split.reverse();
return S.join('')
}
Code: 12:42:26 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
let start = 0;
let end = S.length - 1;
while (start < end) {
}
}
function reverseString(S) {
return S.split('').reverse().join('');
}
Code: 12:43:13 UTC,
js,
autosave
Code: 12:43:23 UTC,
js,
autosave
Code: 12:51:25 UTC,
js,
autosave
Code: 12:51:35 UTC,
js,
autosave
Code: 12:51:54 UTC,
js,
autosave
Code: 12:52:09 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
if (S.length % 2 === 0) {
return -1;
}
}
function reverseString(S) {
return S.split('').reverse().join('');
}
Code: 12:55:24 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
if (S.length % 2 === 0) {
return -1;
}
let
}
function reverseString(S) {
return S.split('').reverse().join('');
}
Code: 12:55:54 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
if (S.length % 2 === 0) {
return -1;
}
let mid = Math.floor(S.length / 2);
let startString = S
}
function reverseString(S) {
return S.split('').reverse().join('');
}
Code: 12:56:04 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
if (S.length % 2 === 0) {
return -1;
}
let mid = Math.floor(S.length / 2);
let startString = S.slice(0, mid)
}
function reverseString(S) {
return S.split('').reverse().join('');
}
Code: 12:57:02 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
if (S.length % 2 === 0) {
return -1;
}
let mid = Math.floor(S.length / 2);
let startString = S.slice(0, mid);
}
function reverseString(S) {
return S.split('').reverse().join('');
}
Code: 12:57:32 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
if (S.length % 2 === 0) {
return -1;
}
let mid = Math.floor(S.length / 2);
let startString = S.slice(0, mid);
let endString = reverseString(S.slice(mid+1));
if
}
function reverseString(S) {
return S.split('').reverse().join('');
}
Code: 12:57:55 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
if (S.length % 2 === 0) {
return -1;
}
let mid = Math.floor(S.length / 2);
let startString = S.slice(0, mid);
let endString = reverseString(S.slice(mid+1));
if (startString === S)
}
function reverseString(S) {
return S.split('').reverse().join('');
}
Code: 12:58:09 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
if (S.length % 2 === 0) {
return -1;
}
if (S.length)
let mid = Math.floor(S.length / 2);
let startString = S.slice(0, mid);
let endString = reverseString(S.slice(mid+1));
if (startString === S)
}
function reverseString(S) {
return S.split('').reverse().join('');
}
Code: 12:58:30 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
if (S.length % 2 === 0) {
return -1;
}
if (S.length === 1) {
return 0;
}
let mid = Math.floor(S.length / 2);
let startString = S.slice(0, mid);
let endString = reverseString(S.slice(mid+1));
if (startString === S)
}
function reverseString(S) {
return S.split('').reverse().join('');
}
Code: 12:59:00 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
if (S.length % 2 === 0) {
return -1;
}
if (S.length === 1) {
return 0;
}
let mid = Math.floor(S.length / 2);
let startString = S.slice(0, mid);
let endString = reverseString(S.slice(mid+1));
if (startString === endString) {
return mid;
}
return -1;
}
function reverseString(S) {
return S.split('').reverse().join('');
}
Code: 12:59:03 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
if (S.length % 2 === 0) {
return -1;
}
if (S.length === 1) {
return 0;
}
let mid = Math.floor(S.length / 2);
let startString = S.slice(0, mid);
let endString = reverseString(S.slice(mid+1));
if (startString === endString) {
return mid;
}
return -1;
}
function reverseString(S) {
return S.split('').reverse().join('');
}
Analysis
Code: 12:59:15 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
if (S.length % 2 === 0) {
return -1;
}
if (S.length === 1) {
return 0;
}
let mid = Math.floor(S.length / 2);
let startString = S.slice(0, mid);
let endString = reverseString(S.slice(mid+1));
if (startString === endString) {
return mid;
}
return -1;
}
function reverseString(S) {
return S.split('').reverse().join('');
}
User test case 1:
['']
User test case 2:
['not']
Analysis
Code: 12:59:26 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
if (S.length % 2 === 0) {
return -1;
}
if (S.length === 1) {
return 0;
}
let mid = Math.floor(S.length / 2);
let startString = S.slice(0, mid);
let endString = reverseString(S.slice(mid+1));
if (startString === endString) {
return mid;
}
return -1;
}
function reverseString(S) {
return S.split('').reverse().join('');
}
User test case 1:
['']
User test case 2:
['not']
Analysis
Code: 12:59:28 UTC,
js,
final,
score: 
100
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
if (S.length % 2 === 0) {
return -1;
}
if (S.length === 1) {
return 0;
}
let mid = Math.floor(S.length / 2);
let startString = S.slice(0, mid);
let endString = reverseString(S.slice(mid+1));
if (startString === endString) {
return mid;
}
return -1;
}
function reverseString(S) {
return S.split('').reverse().join('');
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(length(S))
expand all
Correctness tests
1.
0.068 s
OK
2.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.072 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.076 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
1.
0.072 s
OK
2.
0.068 s
OK
1.
0.072 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
expand all
Performance tests
1.
0.076 s
OK
2.
0.080 s
OK
3.
0.068 s
OK
1.
0.080 s
OK
1.
0.084 s
OK
1.
0.152 s
OK
1.
0.156 s
OK
2.
0.148 s
OK
3.
0.068 s
OK
1.
0.240 s
OK
2.
0.228 s
OK
3.
0.228 s
OK
4.
0.076 s
OK
5.
0.076 s
OK
6.
0.224 s
OK