Write a function:
def 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].
Invalid result type, Integer expected, NilClass foundstdout:
rac car
Invalid result type, Integer expected, NilClass foundstdout:
x
Invalid result type, Integer expected, NilClass foundstdout:
car car
Invalid result type, Integer expected, NilClass foundstdout:
x
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(s)
# write your code in Ruby 2.2
n = s.size
return 0 if n <= 1
middle = n / 2
left = s[0..(middle-1)]
right = s[(middle+1)..(n-1)]
return middle if left.reverse == right
return -1
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(s)
# write your code in Ruby 2.2
n = s.size
return 0 if n <= 1
middle = n / 2
left = s[0..(middle-1)]
right = s[(middle+1)..(n-1)]
return middle if left.reverse == right
return -1
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(s)
# write your code in Ruby 2.2
n = s.size
return 0 if n <= 1
middle = n / 2
left = s[0..(middle-1)]
right = s[(middle+1)..(n-1)]
return middle if left.reverse == right
return -1
end
The following issues have been detected: wrong answers.
For example, for the input '' the solution returned a wrong answer (got 0 expected -1).