Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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 <= 1000s consists of lowercase English letters, vertical bars '|', and asterisks '*'.s contains an even number of vertical bars '|'.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"l|*e*et|c**o|*de|"
"iamprogrammer"
"yo|uar|e**|b|e***au|tifu|l"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2315: Count Asterisks
class Solution {
public int countAsterisks(String s) {
int ans = 0;
for (int i = 0, ok = 1; i < s.length(); ++i) {
char c = s.charAt(i);
if (c == '*') {
ans += ok;
} else if (c == '|') {
ok ^= 1;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2315: Count Asterisks
func countAsterisks(s string) (ans int) {
ok := 1
for _, c := range s {
if c == '*' {
ans += ok
} else if c == '|' {
ok ^= 1
}
}
return
}
# Accepted solution for LeetCode #2315: Count Asterisks
class Solution:
def countAsterisks(self, s: str) -> int:
ans, ok = 0, 1
for c in s:
if c == "*":
ans += ok
elif c == "|":
ok ^= 1
return ans
// Accepted solution for LeetCode #2315: Count Asterisks
impl Solution {
pub fn count_asterisks(s: String) -> i32 {
let mut ans = 0;
let mut ok = 1;
for &c in s.as_bytes() {
if c == b'*' {
ans += ok;
} else if c == b'|' {
ok ^= 1;
}
}
ans
}
}
// Accepted solution for LeetCode #2315: Count Asterisks
function countAsterisks(s: string): number {
let ans = 0;
let ok = 1;
for (const c of s) {
if (c === '*') {
ans += ok;
} else if (c === '|') {
ok ^= 1;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.