3084. Count Substrings Starting and Ending with Given Character Medium

@problem@discussion
#Math#String#Counting



1/**
2 * [3084] Count Substrings Starting and Ending with Given Character
3 *
4 * You are given a string s and a character c. Return the total number of <span data-keyword="substring-nonempty">substrings</span> of s that start and end with c.
5 *  
6 * <strong class="example">Example 1:
7 * <div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
8 * Input: <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s = "abada", c = "a"</span>
9 * Output: <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">6</span>
10 * Explanation: Substrings starting and ending with "a" are: "<u>a</u>bada", "<u>aba</u>da", "<u>abada</u>", "ab<u>a</u>da", "ab<u>ada</u>", "abad<u>a</u>".
11 * </div>
12 * <strong class="example">Example 2:
13 * <div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
14 * Input: <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s = "zzz", c = "z"</span>
15 * Output: <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">6</span>
16 * Explanation: There are a total of 6 substrings in s and all start and end with "z".
17 * </div>
18 *  
19 * Constraints:
20 * 
21 * 	1 <= s.length <= 10^5
22 * 	s and c consist only of lowercase English letters.
23 * 
24 */
25pub struct Solution {}
26
27// problem: https://leetcode.com/problems/count-substrings-starting-and-ending-with-given-character/
28// discuss: https://leetcode.com/problems/count-substrings-starting-and-ending-with-given-character/discuss/?currentPage=1&orderBy=most_votes&query=
29
30// submission codes start here
31
32impl Solution {
33    pub fn count_substrings(s: String, c: char) -> i64 {
34        
35    }
36}
37
38// submission codes end
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn test_3084() {
46    }
47}
48


Back
© 2025 bowen.ge All Rights Reserved.