Created
September 23, 2021 22:35
-
-
Save felipeska/69ad8dd7692f4c6cc381acadcdfe0ea2 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