-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1967-NumberOfStringsThatAppearAsSubstringsInWord.go
More file actions
108 lines (95 loc) · 3.47 KB
/
1967-NumberOfStringsThatAppearAsSubstringsInWord.go
File metadata and controls
108 lines (95 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
// 1967. Number of Strings That Appear as Substrings in Word
// Given an array of strings patterns and a string word,
// return the number of strings in patterns that exist as a substring in word.
// A substring is a contiguous sequence of characters within a string.
// Example 1:
// Input: patterns = ["a","abc","bc","d"], word = "abc"
// Output: 3
// Explanation:
// - "a" appears as a substring in "abc".
// - "abc" appears as a substring in "abc".
// - "bc" appears as a substring in "abc".
// - "d" does not appear as a substring in "abc".
// 3 of the strings in patterns appear as a substring in word.
// Example 2:
// Input: patterns = ["a","b","c"], word = "aaaaabbbbb"
// Output: 2
// Explanation:
// - "a" appears as a substring in "aaaaabbbbb".
// - "b" appears as a substring in "aaaaabbbbb".
// - "c" does not appear as a substring in "aaaaabbbbb".
// 2 of the strings in patterns appear as a substring in word.
// Example 3:
// Input: patterns = ["a","a","a"], word = "ab"
// Output: 3
// Explanation: Each of the patterns appears as a substring in word "ab".
// Constraints:
// 1 <= patterns.length <= 100
// 1 <= patterns[i].length <= 100
// 1 <= word.length <= 100
// patterns[i] and word consist of lowercase English letters.
import "fmt"
import "strings"
func numOfStrings(patterns []string, word string) int {
res := 0
for _, v := range patterns {
if strings.Index(word, v) >= 0 {
res++
}
}
return res
}
func numOfStrings1(patterns []string, word string) int {
res := 0
for _, v := range patterns {
if strings.Contains(word, v) {
res++
}
}
return res
}
func numOfStrings2(patterns []string, word string) int {
res, n, mp := 0, len(word), make(map[string]bool)
for i := 1; i <= n; i++ {
for j := 0; j + i <= n; j++ {
mp[word[j:j + i]] = true
}
}
for _, p := range patterns {
if mp[p] { res++ }
}
return res
}
func main() {
// Example 1:
// Input: patterns = ["a","abc","bc","d"], word = "abc"
// Output: 3
// Explanation:
// - "a" appears as a substring in "abc".
// - "abc" appears as a substring in "abc".
// - "bc" appears as a substring in "abc".
// - "d" does not appear as a substring in "abc".
// 3 of the strings in patterns appear as a substring in word.
fmt.Println(numOfStrings([]string{"a","abc","bc","d"}, "abc")) // 3
// Example 2:
// Input: patterns = ["a","b","c"], word = "aaaaabbbbb"
// Output: 2
// Explanation:
// - "a" appears as a substring in "aaaaabbbbb".
// - "b" appears as a substring in "aaaaabbbbb".
// - "c" does not appear as a substring in "aaaaabbbbb".
// 2 of the strings in patterns appear as a substring in word.
fmt.Println(numOfStrings([]string{"a","b","c"}, "aaaaabbbbb")) // 2
// Example 3:
// Input: patterns = ["a","a","a"], word = "ab"
// Output: 3
// Explanation: Each of the patterns appears as a substring in word "ab".
fmt.Println(numOfStrings([]string{"a","a","a"}, "ab")) // 3
fmt.Println(numOfStrings1([]string{"a","abc","bc","d"}, "abc")) // 3
fmt.Println(numOfStrings1([]string{"a","b","c"}, "aaaaabbbbb")) // 2
fmt.Println(numOfStrings1([]string{"a","a","a"}, "ab")) // 3
fmt.Println(numOfStrings2([]string{"a","abc","bc","d"}, "abc")) // 3
fmt.Println(numOfStrings2([]string{"a","b","c"}, "aaaaabbbbb")) // 2
fmt.Println(numOfStrings2([]string{"a","a","a"}, "ab")) // 3
}