Skip to content

Commit da0bcfd

Browse files
committed
add sol
1 parent ad81353 commit da0bcfd

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

leetcode/daily/1400/sol.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// https://leetcode.com/problems/construct-k-palindrome-strings/description/
2+
3+
package main
4+
5+
import "fmt"
6+
7+
func canConstruct(s string, k int) bool {
8+
// count freq
9+
freq := make(map[rune]int)
10+
for _, c := range s {
11+
freq[c]++
12+
}
13+
14+
// count odd freq
15+
odd := 0
16+
for _, v := range freq {
17+
if v%2 != 0 {
18+
odd++
19+
}
20+
}
21+
22+
// check conditions
23+
return k <= len(s) && odd <= k
24+
}
25+
26+
func canConstruct2(s string, k int) bool {
27+
if len(s) < k {
28+
return false
29+
}
30+
31+
// count freq
32+
freq := make([]int, 26)
33+
for _, c := range s {
34+
freq[c-'a']++
35+
}
36+
37+
// count odd freq
38+
odd := 0
39+
for _, v := range freq {
40+
if v%2 == 1 {
41+
odd++
42+
}
43+
}
44+
45+
if odd > k {
46+
return false
47+
}
48+
49+
return true
50+
}
51+
52+
func main() {
53+
s := "annabelle"
54+
k := 2
55+
56+
fmt.Println(canConstruct2(s, k))
57+
}

0 commit comments

Comments
 (0)