Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Dec 31, 2023
1 parent 6e8cac4 commit 3eb084c
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// https://leetcode.com/problems/largest-substring-between-two-equal-characters/

package main

import (
"fmt"
)

func maxLengthBetweenEqualCharacters(s string) int {
charIdx := make([]int, 26)
for i := range charIdx {
charIdx[i] = -1
}

result := -1

for i, c := range s {
if charIdx[c-'a'] == -1 {
charIdx[c-'a'] = i
} else {
result = max(result, i-charIdx[c-'a']-1)
}
}

return result
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func main() {
s := "cbzxy"

fmt.Println(maxLengthBetweenEqualCharacters(s))
}

0 comments on commit 3eb084c

Please sign in to comment.