-
-
Save go-cristian/bd43a2d518a36f3b21b48efe20622cb4 to your computer and use it in GitHub Desktop.
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
fun main() { | |
println(palindromeIndex("aaa")) | |
} | |
fun palindromeIndex(s: String): Int { | |
val reversed = s.reversed() | |
if (s == reversed) return -1 | |
for (i in 0 until s.length) { | |
var countLeft = 0 | |
var countRight = s.length - 1 | |
var isPalindrome = true | |
if (countLeft == i) { | |
countLeft++ | |
} | |
if (countRight == i) { | |
countRight-- | |
} | |
while (isPalindrome) { | |
if (s[countLeft] == s[countRight]) { | |
if(countLeft >= countRight + 1) | |
return i | |
countLeft++ | |
countRight-- | |
if (countLeft == i) { | |
countLeft++ | |
} | |
if (countRight == i) { | |
countRight-- | |
} | |
} else { | |
isPalindrome = false | |
} | |
} | |
} | |
return -1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment