Your browser (Unknown 0) is no longer supported. Some parts of the website may not work correctly. Please update your browser.

AVAILABLE EXERCISES:

Exercise 9

Bitwise operations (bit-ops)

Exercise 8

Frontend

Exercise 7

Data Structures

Exercise 6

SQL

Exercise 5

Coding skills

Exercise 4

Algorithmic skills

Exercise 3

2017 Contest

Exercise 2

2016 Contest

Exercise 1

2015 Contest

Find a symmetry point of a string, if any.

Spoken language:

编写一个函数

int solution(char *S);

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

编写一个函数

int solution(string &S);

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

编写一个函数

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

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

编写一个函数

func Solution(S string) int

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

编写一个函数

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

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

编写一个函数

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

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

编写一个函数

function solution(S);

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

编写一个函数

fun solution(S: String): Int

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

编写一个函数

function solution(S)

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

编写一个函数

int solution(NSString *S);

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

编写一个函数

function solution(S: PChar): longint;

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

编写一个函数

function solution($S);

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

编写一个函数

sub solution { my ($S)=@_; ... }

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

编写一个函数

def solution(S)

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

编写一个函数

def solution(s)

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

编写一个函数

object Solution { def solution(s: String): Int }

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

编写一个函数

public func solution(_ S : inout String) -> Int

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

编写一个函数

Private Function solution(S As String) As Integer

从给出的字符串 S 中，找到并返回这样一个字符的下标（下标从 0 开始算）， 使得这个字符左边的子字符串，刚好与右边的子字符串相反 （但如果这样的字符不存在的话，返回 −1）。

例如，给出这样一个字符串

"`racecar`"

你的函数应该返回 3，因为对于下标为 3 的字符 e， 其左边相邻的子字符串是 "`rac`"， 而右边相邻的子字符串是 "`car`"。

注： 与空字符串（长度为 0 的字符串）相反的还是一个空字符串。

假定:

- S 长度范围 [0..2,000,000].

Write a function:

int solution(char *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 S is within the range [0..2,000,000].

Write a function:

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:

"`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 S is within the range [0..2,000,000].

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:

"`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 S is within the range [0..2,000,000].

Write a function:

func Solution(S string) int

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

For example, given a string:

"`racecar`"

`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 S is within the range [0..2,000,000].

Write a function:

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

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

For example, given a string:

"`racecar`"

`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 S is within the range [0..2,000,000].

Write a function:

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

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

For example, given a string:

"`racecar`"

`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 S is within the range [0..2,000,000].

Write a function:

function solution(S);

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

For example, given a string:

"`racecar`"

`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 S is within the range [0..2,000,000].

Write a function:

fun solution(S: String): Int

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

For example, given a string:

"`racecar`"

`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 S is within the range [0..2,000,000].

Write a function:

function solution(S)

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

For example, given a string:

"`racecar`"

`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 S is within the range [0..2,000,000].

Write a function:

int solution(NSString *S);

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

For example, given a string:

"`racecar`"

`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 S is within the range [0..2,000,000].

Write a function:

function solution(S: PChar): longint;

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

For example, given a string:

"`racecar`"

`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 S is within the range [0..2,000,000].

Write a function:

function solution($S);

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

For example, given a string:

"`racecar`"

`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 S is within the range [0..2,000,000].

Write a function:

sub solution { my ($S)=@_; ... }

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

For example, given a string:

"`racecar`"

`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 S is within the range [0..2,000,000].

Write a function:

def solution(S)

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

For example, given a string:

"`racecar`"

`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 S is within the range [0..2,000,000].

Write a function:

def solution(s)

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

For example, given a string:

"`racecar`"

`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 S is within the range [0..2,000,000].

Write a function:

object Solution { def solution(s: String): Int }

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

For example, given a string:

"`racecar`"

`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 S is within the range [0..2,000,000].

Write a function:

public func solution(_ S : inout String) -> Int

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

For example, given a string:

"`racecar`"

`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 S is within the range [0..2,000,000].

Write a function:

Private Function solution(S As String) As Integer

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

For example, given a string:

"`racecar`"

`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 S is within the range [0..2,000,000].