g2301_2400.s2315_count_asterisks.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g2301_2400.s2315_count_asterisks;
// #Easy #String #2022_06_26_Time_1_ms_(100.00%)_Space_42.2_MB_(57.14%)
/**
* 2315 - Count Asterisks\.
*
* Easy
*
* You are given a string `s`, where every **two** consecutive vertical bars `'|'` are grouped into a **pair**. In other words, the 1st and 2nd `'|'` make a pair, the 3rd and 4th `'|'` make a pair, and so forth.
*
* Return _the number of_ `'*'` _in_ `s`_, **excluding** the_ `'*'` _between each pair of_ `'|'`.
*
* **Note** that each `'|'` will belong to **exactly** one pair.
*
* **Example 1:**
*
* **Input:** s = "l|\*e\*et|c\*\*o|\*de|"
*
* **Output:** 2
*
* **Explanation:** The considered characters are underlined: "l|\*e\*et|c\*\*o|\*de|".
*
* 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.
*
* **Example 2:**
*
* **Input:** s = "iamprogrammer"
*
* **Output:** 0
*
* **Explanation:** In this example, there are no asterisks in s. Therefore, we return 0.
*
* **Example 3:**
*
* **Input:** s = "yo|uar|e\*\*|b|e\*\*\*au|tifu|l"
*
* **Output:** 5
*
* **Explanation:** The considered characters are underlined: "yo|uar|e\*\*|b|e\*\*\*au|tifu|l".
*
* There are 5 asterisks considered. Therefore, we return 5.
*
* **Constraints:**
*
* * `1 <= s.length <= 1000`
* * `s` consists of lowercase English letters, vertical bars `'|'`, and asterisks `'*'`.
* * `s` contains an **even** number of vertical bars `'|'`.
**/
public class Solution {
public int countAsterisks(String s) {
int c = 0;
int n = s.length();
int i = 0;
while (i < n) {
if (s.charAt(i) == '|') {
i++;
while (s.charAt(i) != '|') {
i++;
}
}
if (s.charAt(i) == '*') {
c++;
}
i++;
}
return c;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy