Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
You are given a string s and a character c. Return the total number of substrings of s that start and end with c.
Example 1:
Input: s = "abada", c = "a"
Output: 6
Explanation: Substrings starting and ending with "a" are: "abada", "abada", "abada", "abada", "abada", "abada".
Example 2:
Input: s = "zzz", c = "z"
Output: 6
Explanation: There are a total of 6 substrings in s and all start and end with "z".
Constraints:
1 <= s.length <= 105s and c consist only of lowercase English letters.Problem summary: You are given a string s and a character c. Return the total number of substrings of s that start and end with c.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
"abada" "a"
"zzz" "z"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3084: Count Substrings Starting and Ending with Given Character
class Solution {
public long countSubstrings(String s, char c) {
long cnt = s.chars().filter(ch -> ch == c).count();
return cnt + cnt * (cnt - 1) / 2;
}
}
// Accepted solution for LeetCode #3084: Count Substrings Starting and Ending with Given Character
func countSubstrings(s string, c byte) int64 {
cnt := int64(strings.Count(s, string(c)))
return cnt + cnt*(cnt-1)/2
}
# Accepted solution for LeetCode #3084: Count Substrings Starting and Ending with Given Character
class Solution:
def countSubstrings(self, s: str, c: str) -> int:
cnt = s.count(c)
return cnt + cnt * (cnt - 1) // 2
// Accepted solution for LeetCode #3084: Count Substrings Starting and Ending with Given Character
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3084: Count Substrings Starting and Ending with Given Character
// class Solution {
// public long countSubstrings(String s, char c) {
// long cnt = s.chars().filter(ch -> ch == c).count();
// return cnt + cnt * (cnt - 1) / 2;
// }
// }
// Accepted solution for LeetCode #3084: Count Substrings Starting and Ending with Given Character
function countSubstrings(s: string, c: string): number {
const cnt = s.split('').filter(ch => ch === c).length;
return cnt + Math.floor((cnt * (cnt - 1)) / 2);
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.