Skip to content

Instantly share code, notes, and snippets.

@felipeska
Created September 23, 2021 22:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save felipeska/69ad8dd7692f4c6cc381acadcdfe0ea2 to your computer and use it in GitHub Desktop.
Save felipeska/69ad8dd7692f4c6cc381acadcdfe0ea2 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