Skip to content

Commit d9f7995

Browse files
committed
feat: add solutions to lc problem: No.1967.Number of Strings That Appear
as Substrings in Word
1 parent ce05895 commit d9f7995

File tree

17 files changed

+628
-15
lines changed

17 files changed

+628
-15
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# [1966. Binary Searchable Numbers in an Unsorted Array](https://leetcode-cn.com/problems/binary-searchable-numbers-in-an-unsorted-array)
2+
3+
[English Version](/solution/1900-1999/1966.Binary%20Searchable%20Numbers%20in%20an%20Unsorted%20Array/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Consider a function that implements an algorithm <strong>similar</strong> to <a href="https://leetcode.com/explore/learn/card/binary-search/" target="_blank">Binary Search</a>. The function has two input parameters: <code>sequence</code> is a sequence of integers, and <code>target</code> is an integer value. The purpose of the function is to find if the <code>target</code> exists in the <code>sequence</code>.</p>
10+
11+
<p>The pseudocode of the function is as follows:</p>
12+
13+
<pre>
14+
func(sequence, target)
15+
while sequence is not empty
16+
<strong>randomly</strong> choose an element from sequence as the pivot
17+
if pivot = target, return <strong>true</strong>
18+
else if pivot &lt; target, remove pivot and all elements to its left from the sequence
19+
else, remove pivot and all elements to its right from the sequence
20+
end while
21+
return <strong>false</strong>
22+
</pre>
23+
24+
<p>When the <code>sequence</code> is sorted, the function works correctly for <strong>all</strong> values. When the <code>sequence</code> is not sorted, the function does not work for all values, but may still work for <strong>some</strong> values.</p>
25+
26+
<p>Given an integer array <code>nums</code>, representing the <code>sequence</code>, that contains <strong>unique</strong> numbers and <strong>may or may not be sorted</strong>, return <em>the number of values that are <strong>guaranteed</strong> to be found using the function, for <strong>every possible</strong> pivot selection</em>.</p>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Example 1:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> nums = [7]
33+
<strong>Output:</strong> 1
34+
<strong>Explanation</strong>:
35+
Searching for value 7 is guaranteed to be found.
36+
Since the sequence has only one element, 7 will be chosen as the pivot. Because the pivot equals the target, the function will return true.
37+
</pre>
38+
39+
<p><strong>Example 2:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong> nums = [-1,5,2]
43+
<strong>Output:</strong> 1
44+
<strong>Explanation</strong>:
45+
Searching for value -1 is guaranteed to be found.
46+
If -1 was chosen as the pivot, the function would return true.
47+
If 5 was chosen as the pivot, 5 and 2 would be removed. In the next loop, the sequence would have only -1 and the function would return true.
48+
If 2 was chosen as the pivot, 2 would be removed. In the next loop, the sequence would have -1 and 5. No matter which number was chosen as the next pivot, the function would find -1 and return true.
49+
50+
Searching for value 5 is NOT guaranteed to be found.
51+
If 2 was chosen as the pivot, -1, 5 and 2 would be removed. The sequence would be empty and the function would return false.
52+
53+
Searching for value 2 is NOT guaranteed to be found.
54+
If 5 was chosen as the pivot, 5 and 2 would be removed. In the next loop, the sequence would have only -1 and the function would return false.
55+
56+
Because only -1 is guaranteed to be found, you should return 1.
57+
</pre>
58+
59+
<p>&nbsp;</p>
60+
<p><strong>Constraints:</strong></p>
61+
62+
<ul>
63+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
64+
<li><code>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
65+
<li>All the values of <code>nums</code> are <strong>unique</strong>.</li>
66+
</ul>
67+
68+
<p>&nbsp;</p>
69+
<p><strong>Follow-up:</strong> If <code>nums</code> has <strong>duplicates</strong>, would you modify your algorithm? If so, how?</p>
70+
71+
72+
## 解法
73+
74+
<!-- 这里可写通用的实现逻辑 -->
75+
76+
<!-- tabs:start -->
77+
78+
### **Python3**
79+
80+
<!-- 这里可写当前语言的特殊实现逻辑 -->
81+
82+
```python
83+
84+
```
85+
86+
### **Java**
87+
88+
<!-- 这里可写当前语言的特殊实现逻辑 -->
89+
90+
```java
91+
92+
```
93+
94+
### **...**
95+
96+
```
97+
98+
```
99+
100+
<!-- tabs:end -->
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# [1966. Binary Searchable Numbers in an Unsorted Array](https://leetcode.com/problems/binary-searchable-numbers-in-an-unsorted-array)
2+
3+
[中文文档](/solution/1900-1999/1966.Binary%20Searchable%20Numbers%20in%20an%20Unsorted%20Array/README.md)
4+
5+
## Description
6+
7+
<p>Consider a function that implements an algorithm <strong>similar</strong> to <a href="https://leetcode.com/explore/learn/card/binary-search/" target="_blank">Binary Search</a>. The function has two input parameters: <code>sequence</code> is a sequence of integers, and <code>target</code> is an integer value. The purpose of the function is to find if the <code>target</code> exists in the <code>sequence</code>.</p>
8+
9+
<p>The pseudocode of the function is as follows:</p>
10+
11+
<pre>
12+
func(sequence, target)
13+
while sequence is not empty
14+
<strong>randomly</strong> choose an element from sequence as the pivot
15+
if pivot = target, return <strong>true</strong>
16+
else if pivot &lt; target, remove pivot and all elements to its left from the sequence
17+
else, remove pivot and all elements to its right from the sequence
18+
end while
19+
return <strong>false</strong>
20+
</pre>
21+
22+
<p>When the <code>sequence</code> is sorted, the function works correctly for <strong>all</strong> values. When the <code>sequence</code> is not sorted, the function does not work for all values, but may still work for <strong>some</strong> values.</p>
23+
24+
<p>Given an integer array <code>nums</code>, representing the <code>sequence</code>, that contains <strong>unique</strong> numbers and <strong>may or may not be sorted</strong>, return <em>the number of values that are <strong>guaranteed</strong> to be found using the function, for <strong>every possible</strong> pivot selection</em>.</p>
25+
26+
<p>&nbsp;</p>
27+
<p><strong>Example 1:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> nums = [7]
31+
<strong>Output:</strong> 1
32+
<strong>Explanation</strong>:
33+
Searching for value 7 is guaranteed to be found.
34+
Since the sequence has only one element, 7 will be chosen as the pivot. Because the pivot equals the target, the function will return true.
35+
</pre>
36+
37+
<p><strong>Example 2:</strong></p>
38+
39+
<pre>
40+
<strong>Input:</strong> nums = [-1,5,2]
41+
<strong>Output:</strong> 1
42+
<strong>Explanation</strong>:
43+
Searching for value -1 is guaranteed to be found.
44+
If -1 was chosen as the pivot, the function would return true.
45+
If 5 was chosen as the pivot, 5 and 2 would be removed. In the next loop, the sequence would have only -1 and the function would return true.
46+
If 2 was chosen as the pivot, 2 would be removed. In the next loop, the sequence would have -1 and 5. No matter which number was chosen as the next pivot, the function would find -1 and return true.
47+
48+
Searching for value 5 is NOT guaranteed to be found.
49+
If 2 was chosen as the pivot, -1, 5 and 2 would be removed. The sequence would be empty and the function would return false.
50+
51+
Searching for value 2 is NOT guaranteed to be found.
52+
If 5 was chosen as the pivot, 5 and 2 would be removed. In the next loop, the sequence would have only -1 and the function would return false.
53+
54+
Because only -1 is guaranteed to be found, you should return 1.
55+
</pre>
56+
57+
<p>&nbsp;</p>
58+
<p><strong>Constraints:</strong></p>
59+
60+
<ul>
61+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
62+
<li><code>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
63+
<li>All the values of <code>nums</code> are <strong>unique</strong>.</li>
64+
</ul>
65+
66+
<p>&nbsp;</p>
67+
<p><strong>Follow-up:</strong> If <code>nums</code> has <strong>duplicates</strong>, would you modify your algorithm? If so, how?</p>
68+
69+
70+
## Solutions
71+
72+
<!-- tabs:start -->
73+
74+
### **Python3**
75+
76+
```python
77+
78+
```
79+
80+
### **Java**
81+
82+
```java
83+
84+
```
85+
86+
### **...**
87+
88+
```
89+
90+
```
91+
92+
<!-- tabs:end -->

solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/README.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ patterns 中有 2 个字符串作为子字符串出现在 word 中。
5656
<li><code>patterns[i]</code> 和 <code>word</code> 由小写英文字母组成</li>
5757
</ul>
5858

59-
6059
## 解法
6160

6261
<!-- 这里可写通用的实现逻辑 -->
@@ -68,15 +67,56 @@ patterns 中有 2 个字符串作为子字符串出现在 word 中。
6867
<!-- 这里可写当前语言的特殊实现逻辑 -->
6968

7069
```python
71-
70+
class Solution:
71+
def numOfStrings(self, patterns: List[str], word: str) -> int:
72+
return sum(1 for p in patterns if p in word)
7273
```
7374

7475
### **Java**
7576

7677
<!-- 这里可写当前语言的特殊实现逻辑 -->
7778

7879
```java
80+
class Solution {
81+
public int numOfStrings(String[] patterns, String word) {
82+
int res = 0;
83+
for (String p : patterns) {
84+
if (word.contains(p)) {
85+
++res;
86+
}
87+
}
88+
return res;
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
int numOfStrings(vector<string> &patterns, string word) {
99+
int res = 0;
100+
for (auto p : patterns)
101+
if (word.find(p) != string::npos)
102+
++res;
103+
return res;
104+
}
105+
};
106+
```
79107
108+
### **Go**
109+
110+
```go
111+
func numOfStrings(patterns []string, word string) int {
112+
res := 0
113+
for _, p := range patterns {
114+
if strings.Contains(word, p) {
115+
res++
116+
}
117+
}
118+
return res
119+
}
80120
```
81121

82122
### **...**

solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/README_EN.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,61 @@
5252
<li><code>patterns[i]</code> and <code>word</code> consist of lowercase English letters.</li>
5353
</ul>
5454

55-
5655
## Solutions
5756

5857
<!-- tabs:start -->
5958

6059
### **Python3**
6160

6261
```python
63-
62+
class Solution:
63+
def numOfStrings(self, patterns: List[str], word: str) -> int:
64+
return sum(1 for p in patterns if p in word)
6465
```
6566

6667
### **Java**
6768

6869
```java
70+
class Solution {
71+
public int numOfStrings(String[] patterns, String word) {
72+
int res = 0;
73+
for (String p : patterns) {
74+
if (word.contains(p)) {
75+
++res;
76+
}
77+
}
78+
return res;
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
int numOfStrings(vector<string> &patterns, string word) {
89+
int res = 0;
90+
for (auto p : patterns)
91+
if (word.find(p) != string::npos)
92+
++res;
93+
return res;
94+
}
95+
};
96+
```
6997
98+
### **Go**
99+
100+
```go
101+
func numOfStrings(patterns []string, word string) int {
102+
res := 0
103+
for _, p := range patterns {
104+
if strings.Contains(word, p) {
105+
res++
106+
}
107+
}
108+
return res
109+
}
70110
```
71111

72112
### **...**
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int numOfStrings(vector<string> &patterns, string word) {
4+
int res = 0;
5+
for (auto p : patterns)
6+
if (word.find(p) != string::npos)
7+
++res;
8+
return res;
9+
}
10+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func numOfStrings(patterns []string, word string) int {
2+
res := 0
3+
for _, p := range patterns {
4+
if strings.Contains(word, p) {
5+
res++
6+
}
7+
}
8+
return res
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int numOfStrings(String[] patterns, String word) {
3+
int res = 0;
4+
for (String p : patterns) {
5+
if (word.contains(p)) {
6+
++res;
7+
}
8+
}
9+
return res;
10+
}
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def numOfStrings(self, patterns: List[str], word: str) -> int:
3+
return sum(1 for p in patterns if p in word)

0 commit comments

Comments
 (0)