Skip to content

Commit b4f169b

Browse files
committed
feat: add solutions to lc problems: No.2315~2322
* No.2315.Count Asterisks * No.2316.Count Unreachable Pairs of Nodes in an Undirected Graph * No.2317.Maximum XOR After Operations * No.2318.Number of Distinct Roll Sequences * No.2319.Check if Matrix Is X-Matrix * No.2320.Count Number of Ways to Place Houses * No.2321.Maximum Score Of Spliced Array * No.2322.Minimum Score After Removals on a Tree
1 parent c859804 commit b4f169b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+3799
-1
lines changed

solution/0700-0799/0710.Random Pick with Blacklist/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class Solution:
2-
32
def __init__(self, n: int, blacklist: List[int]):
43
self.k = n - len(blacklist)
54
self.d = {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# [2315. 统计星号](https://leetcode.cn/problems/count-asterisks)
2+
3+
[English Version](/solution/2300-2399/2315.Count%20Asterisks/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个字符串&nbsp;<code>s</code>&nbsp;,每&nbsp;<strong>两个</strong>&nbsp;连续竖线&nbsp;<code>'|'</code>&nbsp;为 <strong>一对</strong>&nbsp;。换言之,第一个和第二个&nbsp;<code>'|'</code>&nbsp;为一对,第三个和第四个&nbsp;<code>'|'</code>&nbsp;为一对,以此类推。</p>
10+
11+
<p>请你返回 <strong>不在</strong> 竖线对之间,<code>s</code>&nbsp;&nbsp;<code>'*'</code>&nbsp;的数目。</p>
12+
13+
<p><strong>注意</strong>,每个竖线&nbsp;<code>'|'</code>&nbsp;都会 <strong>恰好</strong>&nbsp;属于一个对。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong>示例 1:</strong></p>
18+
19+
<pre><b>输入:</b>s = "l|*e*et|c**o|*de|"
20+
<b>输出:</b>2
21+
<b>解释:</b>不在竖线对之间的字符加粗加斜体后,得到字符串:"<strong><em>l</em></strong>|*e*et|<strong><em>c**o</em></strong>|*de|" 。
22+
第一和第二条竖线 '|' 之间的字符不计入答案。
23+
同时,第三条和第四条竖线 '|' 之间的字符也不计入答案。
24+
不在竖线对之间总共有 2 个星号,所以我们返回 2 。</pre>
25+
26+
<p><strong>示例 2:</strong></p>
27+
28+
<pre><b>输入:</b>s = "iamprogrammer"
29+
<b>输出:</b>0
30+
<b>解释:</b>在这个例子中,s 中没有星号。所以返回 0 。
31+
</pre>
32+
33+
<p><strong>示例 3:</strong></p>
34+
35+
<pre><b>输入:</b>s = "yo|uar|e**|b|e***au|tifu|l"
36+
<b>输出:</b>5
37+
<b>解释:</b>需要考虑的字符加粗加斜体后:"<strong><em>yo</em></strong>|uar|<strong><em>e**</em></strong>|b|<strong><em>e***au</em></strong>|tifu|<strong><em>l</em></strong>" 。不在竖线对之间总共有 5 个星号。所以我们返回 5 。</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p><strong>提示:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
45+
<li><code>s</code>&nbsp;只包含小写英文字母,竖线&nbsp;<code>'|'</code>&nbsp;和星号&nbsp;<code>'*'</code>&nbsp;。</li>
46+
<li><code>s</code>&nbsp;包含 <strong>偶数</strong>&nbsp;个竖线&nbsp;<code>'|'</code> 。</li>
47+
</ul>
48+
49+
## 解法
50+
51+
<!-- 这里可写通用的实现逻辑 -->
52+
53+
<!-- tabs:start -->
54+
55+
### **Python3**
56+
57+
<!-- 这里可写当前语言的特殊实现逻辑 -->
58+
59+
```python
60+
class Solution:
61+
def countAsterisks(self, s: str) -> int:
62+
ans = t = 0
63+
for c in s:
64+
if c == '|':
65+
t ^= 1
66+
elif c == '*':
67+
if t == 0:
68+
ans += 1
69+
return ans
70+
```
71+
72+
### **Java**
73+
74+
<!-- 这里可写当前语言的特殊实现逻辑 -->
75+
76+
```java
77+
class Solution {
78+
public int countAsterisks(String s) {
79+
int ans = 0, t = 0;
80+
for (char c : s.toCharArray()) {
81+
if (c == '|') {
82+
t ^= 1;
83+
} else if (c == '*') {
84+
if (t == 0) {
85+
++ans;
86+
}
87+
}
88+
}
89+
return ans;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int countAsterisks(string s) {
100+
int ans = 0, t = 0;
101+
for (char& c : s)
102+
{
103+
if (c == '|') t ^= 1;
104+
else if (c == '*') ans += t == 0;
105+
}
106+
return ans;
107+
}
108+
};
109+
```
110+
111+
### **Go**
112+
113+
```go
114+
func countAsterisks(s string) int {
115+
ans, t := 0, 0
116+
for _, c := range s {
117+
if c == '|' {
118+
t ^= 1
119+
} else if c == '*' {
120+
if t == 0 {
121+
ans++
122+
}
123+
}
124+
}
125+
return ans
126+
}
127+
```
128+
129+
### **TypeScript**
130+
131+
```ts
132+
133+
```
134+
135+
### **...**
136+
137+
```
138+
139+
```
140+
141+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# [2315. Count Asterisks](https://leetcode.com/problems/count-asterisks)
2+
3+
[中文文档](/solution/2300-2399/2315.Count%20Asterisks/README.md)
4+
5+
## Description
6+
7+
<p>You are given a string <code>s</code>, where every <strong>two</strong> consecutive vertical bars <code>&#39;|&#39;</code> are grouped into a <strong>pair</strong>. In other words, the 1<sup>st</sup> and 2<sup>nd</sup> <code>&#39;|&#39;</code> make a pair, the 3<sup>rd</sup> and 4<sup>th</sup> <code>&#39;|&#39;</code> make a pair, and so forth.</p>
8+
9+
<p>Return <em>the number of </em><code>&#39;*&#39;</code><em> in </em><code>s</code><em>, <strong>excluding</strong> the </em><code>&#39;*&#39;</code><em> between each pair of </em><code>&#39;|&#39;</code>.</p>
10+
11+
<p><strong>Note</strong> that each <code>&#39;|&#39;</code> will belong to <strong>exactly</strong> one pair.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> s = &quot;l|*e*et|c**o|*de|&quot;
18+
<strong>Output:</strong> 2
19+
<strong>Explanation:</strong> The considered characters are underlined: &quot;<u>l</u>|*e*et|<u>c**o</u>|*de|&quot;.
20+
The characters between the first and second &#39;|&#39; are excluded from the answer.
21+
Also, the characters between the third and fourth &#39;|&#39; are excluded from the answer.
22+
There are 2 asterisks considered. Therefore, we return 2.</pre>
23+
24+
<p><strong>Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> s = &quot;iamprogrammer&quot;
28+
<strong>Output:</strong> 0
29+
<strong>Explanation:</strong> In this example, there are no asterisks in s. Therefore, we return 0.
30+
</pre>
31+
32+
<p><strong>Example 3:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> s = &quot;yo|uar|e**|b|e***au|tifu|l&quot;
36+
<strong>Output:</strong> 5
37+
<strong>Explanation:</strong> The considered characters are underlined: &quot;<u>yo</u>|uar|<u>e**</u>|b|<u>e***au</u>|tifu|<u>l</u>&quot;. There are 5 asterisks considered. Therefore, we return 5.</pre>
38+
39+
<p>&nbsp;</p>
40+
<p><strong>Constraints:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
44+
<li><code>s</code> consists of lowercase English letters, vertical bars <code>&#39;|&#39;</code>, and asterisks <code>&#39;*&#39;</code>.</li>
45+
<li><code>s</code> contains an <strong>even</strong> number of vertical bars <code>&#39;|&#39;</code>.</li>
46+
</ul>
47+
48+
49+
## Solutions
50+
51+
<!-- tabs:start -->
52+
53+
### **Python3**
54+
55+
```python
56+
class Solution:
57+
def countAsterisks(self, s: str) -> int:
58+
ans = t = 0
59+
for c in s:
60+
if c == '|':
61+
t ^= 1
62+
elif c == '*':
63+
if t == 0:
64+
ans += 1
65+
return ans
66+
```
67+
68+
### **Java**
69+
70+
```java
71+
class Solution {
72+
public int countAsterisks(String s) {
73+
int ans = 0, t = 0;
74+
for (char c : s.toCharArray()) {
75+
if (c == '|') {
76+
t ^= 1;
77+
} else if (c == '*') {
78+
if (t == 0) {
79+
++ans;
80+
}
81+
}
82+
}
83+
return ans;
84+
}
85+
}
86+
```
87+
88+
### **C++**
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
int countAsterisks(string s) {
94+
int ans = 0, t = 0;
95+
for (char& c : s)
96+
{
97+
if (c == '|') t ^= 1;
98+
else if (c == '*') ans += t == 0;
99+
}
100+
return ans;
101+
}
102+
};
103+
```
104+
105+
### **Go**
106+
107+
```go
108+
func countAsterisks(s string) int {
109+
ans, t := 0, 0
110+
for _, c := range s {
111+
if c == '|' {
112+
t ^= 1
113+
} else if c == '*' {
114+
if t == 0 {
115+
ans++
116+
}
117+
}
118+
}
119+
return ans
120+
}
121+
```
122+
123+
### **TypeScript**
124+
125+
```ts
126+
127+
```
128+
129+
### **...**
130+
131+
```
132+
133+
```
134+
135+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int countAsterisks(string s) {
4+
int ans = 0, t = 0;
5+
for (char& c : s)
6+
{
7+
if (c == '|') t ^= 1;
8+
else if (c == '*') ans += t == 0;
9+
}
10+
return ans;
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func countAsterisks(s string) int {
2+
ans, t := 0, 0
3+
for _, c := range s {
4+
if c == '|' {
5+
t ^= 1
6+
} else if c == '*' {
7+
if t == 0 {
8+
ans++
9+
}
10+
}
11+
}
12+
return ans
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int countAsterisks(String s) {
3+
int ans = 0, t = 0;
4+
for (char c : s.toCharArray()) {
5+
if (c == '|') {
6+
t ^= 1;
7+
} else if (c == '*') {
8+
if (t == 0) {
9+
++ans;
10+
}
11+
}
12+
}
13+
return ans;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def countAsterisks(self, s: str) -> int:
3+
ans = t = 0
4+
for c in s:
5+
if c == '|':
6+
t ^= 1
7+
elif c == '*':
8+
if t == 0:
9+
ans += 1
10+
return ans

0 commit comments

Comments
 (0)