3803. Count Residue Prefixes Easy

@problem@discussion
#Hash Table#String



1/**
2 * [3803] Count Residue Prefixes
3 *
4 * You are given a string s consisting only of lowercase English letters.
5 * A prefix of s is called a residue if the number of distinct characters in the prefix is equal to len(prefix) % 3.
6 * Return the count of residue prefixes in s.
7 * A prefix of a string is a non-empty substring that starts from the beginning of the string and extends to any point within it.
8 *  
9 * <strong class="example">Example 1:
10 * <div class="example-block">
11 * Input: <span class="example-io">s = "abc"</span>
12 * Output: <span class="example-io">2</span>
13 * Explanation:​​​​​​​
14 * 
15 * 	Prefix "a" has 1 distinct character and length modulo 3 is 1, so it is a residue.
16 * 	Prefix "ab" has 2 distinct characters and length modulo 3 is 2, so it is a residue.
17 * 	Prefix "abc" does not satisfy the condition. Thus, the answer is 2.
18 * </div>
19 * <strong class="example">Example 2:
20 * <div class="example-block">
21 * Input: <span class="example-io">s = "dd"</span>
22 * Output: <span class="example-io">1</span>
23 * Explanation:
24 * 
25 * 	Prefix "d" has 1 distinct character and length modulo 3 is 1, so it is a residue.
26 * 	Prefix "dd" has 1 distinct character but length modulo 3 is 2, so it is not a residue. Thus, the answer is 1.
27 * </div>
28 * <strong class="example">Example 3:
29 * <div class="example-block">
30 * Input: <span class="example-io">s = "bob"</span>
31 * Output: <span class="example-io">2</span>
32 * Explanation:
33 * 
34 * 	Prefix "b" has 1 distinct character and length modulo 3 is 1, so it is a residue.
35 * 	Prefix "bo" has 2 distinct characters and length mod 3 is 2, so it is a residue. Thus, the answer is 2.
36 * </div>
37 *  
38 * Constraints:
39 * 
40 * 	1 <= s.length <= 100
41 * 	s contains only lowercase English letters.
42 * 
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/count-residue-prefixes/
47// discuss: https://leetcode.com/problems/count-residue-prefixes/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52    pub fn residue_prefixes(s: String) -> i32 {
53        0
54    }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_3803() {
65    }
66}
67

Back
© 2026 bowen.ge All Rights Reserved.