Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Jan 11, 2025
1 parent ad81353 commit da0bcfd
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions leetcode/daily/1400/sol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// https://leetcode.com/problems/construct-k-palindrome-strings/description/

package main

import "fmt"

func canConstruct(s string, k int) bool {
// count freq
freq := make(map[rune]int)
for _, c := range s {
freq[c]++
}

// count odd freq
odd := 0
for _, v := range freq {
if v%2 != 0 {
odd++
}
}

// check conditions
return k <= len(s) && odd <= k
}

func canConstruct2(s string, k int) bool {
if len(s) < k {
return false
}

// count freq
freq := make([]int, 26)
for _, c := range s {
freq[c-'a']++
}

// count odd freq
odd := 0
for _, v := range freq {
if v%2 == 1 {
odd++
}
}

if odd > k {
return false
}

return true
}

func main() {
s := "annabelle"
k := 2

fmt.Println(canConstruct2(s, k))
}

0 comments on commit da0bcfd

Please sign in to comment.