Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Jan 12, 2025
1 parent da0bcfd commit 7428ed3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
46 changes: 46 additions & 0 deletions leetcode/daily/2116/sol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/

package main

import "fmt"

func canBeValid(s string, locked string) bool {
if len(s)%2 != 0 {
return false
}

// Forward pass
balance := 0
for i := 0; i < len(s); i++ {
if locked[i] == '1' && s[i] == ')' {
balance--
} else {
balance++
}
if balance < 0 {
return false
}
}

// Backward pass
balance = 0
for i := len(s) - 1; i >= 0; i-- {
if locked[i] == '1' && s[i] == '(' {
balance--
} else {
balance++
}
if balance < 0 {
return false
}
}

return true
}

func main() {
s := "))()))"
locked := "010100"

fmt.Println(canBeValid(s, locked))
}
48 changes: 48 additions & 0 deletions leetcode/daily/916/sol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// https://leetcode.com/problems/word-subsets/description/

package main

import "fmt"

func wordSubsets(words1 []string, words2 []string) []string {
// count freq
countFrequencies := func(word string) [26]int {
freq := [26]int{}
for _, c := range word {
freq[c-'a']++
}
return freq
}

// combine max words2
maxFreq := [26]int{}
for _, word := range words2 {
freq := countFrequencies(word)
for i := 0; i < 26; i++ {
maxFreq[i] = max(maxFreq[i], freq[i])
}
}

result := []string{}
for _, word := range words1 {
freq := countFrequencies(word)
isUniversal := true
for i := 0; i < 26; i++ {
if freq[i] < maxFreq[i] {
isUniversal = false
break
}
}
if isUniversal {
result = append(result, word)
}
}

return result
}

func main() {
words1 := []string{"amazon", "apple", "facebook", "google", "leetcode"}
words2 := []string{"e", "o"}
fmt.Println(wordSubsets(words1, words2))
}

0 comments on commit 7428ed3

Please sign in to comment.