Created
October 9, 2015 05:35
-
-
Save feliperazeek/21eb0b82076e59c3c26a to your computer and use it in GitHub Desktop.
Codility StrSymmetryPoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Solution { | |
def solution(S: String): Int = { | |
val max = S.length / 2 | |
@scala.annotation.tailrec | |
def symmetryPoint(i: Int): Int = { | |
val left = S(i) | |
val right = S(S.length - 1 - i) | |
if (left == right && i == max) i | |
else if (left == right && i < max) symmetryPoint(i + 1) | |
else i - 1 | |
} | |
if (S.length < 1 || S.length > 2000000 || S.length % 2 != 1) -1 | |
else symmetryPoint(i = 0) | |
} | |
def main(args: Array[String]) = { | |
println("X: " + solution("racecar")) | |
println("X: " + solution("aaaaaaa")) | |
println("X: " + solution("0")) | |
println("X: " + solution("")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment