Question: https://codility.com/demo/take-sample-test/str_symmetry_point
Question Name: Str-Symmetry-Point or StrSymmetryPoint
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | def solution(S): sLen = len(S) # Symmetry point is possible, when and only when the # string's length is odd. if sLen % 2 == 0: return -1 # With a odd-length string, the only possible symmetry # point is the middle point. mid = sLen // 2 begin, end = 0, sLen-1 # The middle point of an odd-length string is symmetry # point, only when the string is symmetry. while begin < mid: if S[begin] != S[end]: return -1 begin += 1 end -= 1 return mid |
I know that it’s a super easy problem but here’s my C++ solution:
here’s my php solution 🙂
a bit pythonic solution:
can do a %2 check as well before going into the loop
It’s logically right. The the performance does not meet the requirement.