Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions solution/2370/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

func longestIdealString(s string, k int) int {
const maxChars = 26
dp := make([]int, maxChars)
result := 0

for _, ch := range s {
maxLen := 0
// Compute the max dp value from possible predecessors within range defined by k
for d := 0; d <= k; d++ {
if int(ch)-d >= 'a' {
maxLen = max(maxLen, dp[int(ch)-'a'-d])
}
if int(ch)+d <= 'z' {
maxLen = max(maxLen, dp[int(ch)-'a'+d])
}
}
// Update dp value for current character
dp[int(ch)-'a'] = maxLen + 1
// Update result to keep track of the maximum length encountered so far
result = max(result, dp[int(ch)-'a'])
}

return result
}

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