Skip to content

Commit 92640ff

Browse files
authored
Create 1624-largest-substring-between-two-equal-characters.kt
1 parent 6d17466 commit 92640ff

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
fun maxLengthBetweenEqualCharacters(s: String): Int {
3+
val charIndex = HashMap<Char, Int>()
4+
var res = -1
5+
6+
for ((i, c) in s.withIndex()) {
7+
if (c in charIndex)
8+
res = maxOf(res, i - charIndex[c]!! - 1)
9+
else
10+
charIndex[c] = i
11+
}
12+
13+
return res
14+
}
15+
}
16+
17+
// Similar but slightly different way
18+
class Solution {
19+
fun maxLengthBetweenEqualCharacters(s: String): Int {
20+
val letters = Array (26) { intArrayOf(302, -302) }
21+
var res = -1
22+
for (l in letters.indices) {
23+
for ((i, c) in s.withIndex()) {
24+
if (c == ('a' + l)) {
25+
letters[l][0] = minOf(letters[l][0], i)
26+
letters[l][1] = maxOf(letters[l][1], i)
27+
res = maxOf(res, letters[l][1] - letters[l][0] - 1)
28+
}
29+
}
30+
}
31+
32+
return res
33+
}
34+
}

0 commit comments

Comments
 (0)