Skip to content

Commit eb90f67

Browse files
authored
Added task 2315.
1 parent 65b9f12 commit eb90f67

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.11'
14851485

14861486
| # | Title | Difficulty | Tag | Time, ms | Time, %
14871487
|------|----------------|-------------|-------------|----------|---------
1488+
| 2315 |[Count Asterisks](src/main/java/g2301_2400/s2315_count_asterisks/Solution.java)| Easy || 1 | 100.00
14881489
| 2312 |[Selling Pieces of Wood](src/main/java/g2301_2400/s2312_selling_pieces_of_wood/Solution.java)| Hard | Backtracking | 78 | 63.64
14891490
| 2311 |[Longest Binary Subsequence Less Than or Equal to K](src/main/java/g2301_2400/s2311_longest_binary_subsequence_less_than_or_equal_to_k/Solution.java)| Medium | String, Dynamic_Programming, Greedy, Memoization | 1 | 100.00
14901491
| 2310 |[Sum of Numbers With Units Digit K](src/main/java/g2301_2400/s2310_sum_of_numbers_with_units_digit_k/Solution.java)| Medium | Math | 1 | 66.67
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2301_2400.s2315_count_asterisks;
2+
3+
// #Easy #2022_06_26_Time_1_ms_(100.00%)_Space_42.2_MB_(57.14%)
4+
5+
public class Solution {
6+
public int countAsterisks(String s) {
7+
int c = 0;
8+
int n = s.length();
9+
int i = 0;
10+
while (i < n) {
11+
if (s.charAt(i) == '|') {
12+
i++;
13+
while (s.charAt(i) != '|') {
14+
i++;
15+
}
16+
}
17+
if (s.charAt(i) == '*') {
18+
c++;
19+
}
20+
i++;
21+
}
22+
return c;
23+
}
24+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2315\. Count Asterisks
2+
3+
Easy
4+
5+
You are given a string `s`, where every **two** consecutive vertical bars `'|'` are grouped into a **pair**. In other words, the 1<sup>st</sup> and 2<sup>nd</sup> `'|'` make a pair, the 3<sup>rd</sup> and 4<sup>th</sup> `'|'` make a pair, and so forth.
6+
7+
Return _the number of_ `'*'` _in_ `s`_, **excluding** the_ `'*'` _between each pair of_ `'|'`.
8+
9+
**Note** that each `'|'` will belong to **exactly** one pair.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "l|\*e\*et|c\*\*o|\*de|"
14+
15+
**Output:** 2
16+
17+
**Explanation:** The considered characters are underlined: "l|\*e\*et|c\*\*o|\*de|".
18+
19+
The characters between the first and second '|' are excluded from the answer. Also, the characters between the third and fourth '|' are excluded from the answer. There are 2 asterisks considered. Therefore, we return 2.
20+
21+
**Example 2:**
22+
23+
**Input:** s = "iamprogrammer"
24+
25+
**Output:** 0
26+
27+
**Explanation:** In this example, there are no asterisks in s. Therefore, we return 0.
28+
29+
**Example 3:**
30+
31+
**Input:** s = "yo|uar|e\*\*|b|e\*\*\*au|tifu|l"
32+
33+
**Output:** 5
34+
35+
**Explanation:** The considered characters are underlined: "yo|uar|e\*\*|b|e\*\*\*au|tifu|l".
36+
37+
There are 5 asterisks considered. Therefore, we return 5.
38+
39+
**Constraints:**
40+
41+
* `1 <= s.length <= 1000`
42+
* `s` consists of lowercase English letters, vertical bars `'|'`, and asterisks `'*'`.
43+
* `s` contains an **even** number of vertical bars `'|'`.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2301_2400.s2315_count_asterisks;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void countAsterisks() {
11+
assertThat(new Solution().countAsterisks("l|*e*et|c**o|*de|"), equalTo(2));
12+
}
13+
14+
@Test
15+
void countAsterisks2() {
16+
assertThat(new Solution().countAsterisks("iamprogrammer"), equalTo(0));
17+
}
18+
19+
@Test
20+
void countAsterisks3() {
21+
assertThat(new Solution().countAsterisks("yo|uar|e**|b|e***au|tifu|l"), equalTo(5));
22+
}
23+
}

0 commit comments

Comments
 (0)