Created
March 13, 2021 13:50
-
-
Save GeorgCantor/230142941a97f4cc15bfcb93f3647eed 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 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