Skip to content

Instantly share code, notes, and snippets.

@go-cristian
Forked from felipeska/PalindromeIndex.kt
Created September 23, 2021 22:36
Show Gist options
  • Save go-cristian/bd43a2d518a36f3b21b48efe20622cb4 to your computer and use it in GitHub Desktop.
Save go-cristian/bd43a2d518a36f3b21b48efe20622cb4 to your computer and use it in GitHub Desktop.
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