Skip to content

Instantly share code, notes, and snippets.

@GeorgCantor
Created March 13, 2021 13:50
Show Gist options
  • Save GeorgCantor/230142941a97f4cc15bfcb93f3647eed to your computer and use it in GitHub Desktop.
Save GeorgCantor/230142941a97f4cc15bfcb93f3647eed to your computer and use it in GitHub Desktop.
fun countSubstrings(s: String): Int {
var c = 0
for (i in s.indices) {
val sb = StringBuilder()
for (j in i until s.length) if (isPal(sb.append(s[j]).toString())) c++
}
return c
}
private fun isPal(word: String): Boolean {
var l = 0
var r = word.lastIndex
while (l < r) if (word[l++] != word[r--]) return false
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment