Skip to content

Commit f7decc8

Browse files
solves #2315: Count Asterisks in java
1 parent 7270c6c commit f7decc8

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@
744744
| 2300 | [Successful Pairs of Spells and Potions](https://leetcode.com/problems/successful-pairs-of-spells-and-potions) | [![Python](assets/python.png)](python/successfull_pairs_of_spells_and_potions.py) | |
745745
| 2303 | [Calculate Amount Paid in Taxes](https://leetcode.com/problems/calculate-amount-paid-in-taxes) | [![Java](assets/java.png)](src/CalculateAmountPaidInTaxes.java) | |
746746
| 2309 | [Greatest English Letter in Upper and Lower Case](https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case) | [![Java](assets/java.png)](src/GreatestEnglishLetterInUpperAndLowerCase.java) | |
747-
| 2315 | [Count Asterisks](https://leetcode.com/problems/count-asterisks) | [![Python](assets/python.png)](python/successfull_pairs_of_spells_and_potions.py) | |
747+
| 2315 | [Count Asterisks](https://leetcode.com/problems/count-asterisks) | [![Java](assets/java.png)](src/CountAsterisks.java) [![Python](assets/python.png)](python/successfull_pairs_of_spells_and_potions.py) | |
748748
| 2316 | [Count Unreachable Pairs of Nodes in an Undirected Graph](https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |
749749
| 2319 | [Check if Matrix Is X-Matrix](https://leetcode.com/problems/check-if-matrix-is-x-matrix) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |
750750
| 2325 | [Decode the Message](https://leetcode.com/problems/decode-the-message) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |

Diff for: src/CountAsterisks.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// https://leetcode.com/problems/count-asterisks
2+
// T: O(|s|)
3+
// S: O(1)
4+
5+
public class CountAsterisks {
6+
public int countAsterisks(String s) {
7+
boolean seenPipe = false;
8+
int count = 0, pipeBuffer = 0;
9+
10+
for (int index = 0 ; index < s.length() ; index++) {
11+
if (s.charAt(index) == '|') {
12+
if (seenPipe) {
13+
pipeBuffer = 0;
14+
}
15+
seenPipe = !seenPipe;
16+
}
17+
18+
if (s.charAt(index) == '*') {
19+
if (seenPipe) pipeBuffer++;
20+
else count++;
21+
}
22+
}
23+
24+
return count + pipeBuffer;
25+
}
26+
}

0 commit comments

Comments
 (0)