Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fc6d8b5

Browse files
author
Openset
committed
Add: Ransom Note
1 parent 031cea6 commit fc6d8b5

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

‎problems/ransom-note/ransom_note.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
11
package ransom_note
2+
3+
func canConstruct(ransomNote string, magazine string) bool {
4+
var m [128]int
5+
for _, c := range magazine {
6+
m[c]++
7+
}
8+
for _, c := range ransomNote {
9+
m[c]--
10+
if m[c] < 0 {
11+
return false
12+
}
13+
}
14+
return true
15+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
11
package ransom_note
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
ransom string
7+
magazine string
8+
expected bool
9+
}
10+
11+
func TestCanConstruct(t *testing.T) {
12+
tests := [...]caseType{
13+
{
14+
ransom: "a",
15+
magazine: "b",
16+
expected: false,
17+
},
18+
{
19+
ransom: "aa",
20+
magazine: "ab",
21+
expected: false,
22+
},
23+
{
24+
ransom: "aa",
25+
magazine: "aab",
26+
expected: true,
27+
},
28+
}
29+
for _, tc := range tests {
30+
output := canConstruct(tc.ransom, tc.magazine)
31+
if output != tc.expected {
32+
t.Fatalf("input: %v %v, output: %v, expected: %v", tc.ransom, tc.magazine, output, tc.expected)
33+
}
34+
}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /