Skip to content

Instantly share code, notes, and snippets.

@RotBolt
Created June 9, 2019 09:21
Show Gist options
  • Save RotBolt/d02ff1d59bdb8c1cd286cd760a119522 to your computer and use it in GitHub Desktop.
Save RotBolt/d02ff1d59bdb8c1cd286cd760a119522 to your computer and use it in GitHub Desktop.
private fun String.lastIndexOf(char: Char): Int {
var bOpen = 0 // count for open bracket
var bClose = 0 // count for close bracket
for (i in this.indices) {
val currChar = this[i]
when {
currChar == char && bOpen == bClose ->
return this.length - i - 1
currChar == '(' -> bOpen++
currChar == ')' -> bClose++
}
}
return -1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment