Skip to content

Commit

Permalink
Create 1624-largest-substring-between-two-equal-characters.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a authored Jan 4, 2024
1 parent 6d17466 commit 92640ff
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions kotlin/1624-largest-substring-between-two-equal-characters.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
fun maxLengthBetweenEqualCharacters(s: String): Int {
val charIndex = HashMap<Char, Int>()
var res = -1

for ((i, c) in s.withIndex()) {
if (c in charIndex)
res = maxOf(res, i - charIndex[c]!! - 1)
else
charIndex[c] = i
}

return res
}
}

// Similar but slightly different way
class Solution {
fun maxLengthBetweenEqualCharacters(s: String): Int {
val letters = Array (26) { intArrayOf(302, -302) }
var res = -1
for (l in letters.indices) {
for ((i, c) in s.withIndex()) {
if (c == ('a' + l)) {
letters[l][0] = minOf(letters[l][0], i)
letters[l][1] = maxOf(letters[l][1], i)
res = maxOf(res, letters[l][1] - letters[l][0] - 1)
}
}
}

return res
}
}

0 comments on commit 92640ff

Please sign in to comment.