File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments