2730. Find the Longest Semi-Repetitive Substring Medium

@problem@discussion
#String#Sliding Window



1/**
2 * [2730] Find the Longest Semi-Repetitive Substring
3 *
4 * You are given a digit string s that consists of digits from 0 to 9.
5 * A string is called semi-repetitive if there is at most one adjacent pair of the same digit. For example, "0010", "002020", "0123", "2002", and "54944" are semi-repetitive while the following are not: "00101022" (adjacent same digit pairs are 00 and 22), and "1101234883" (adjacent same digit pairs are 11 and 88).
6 * Return the length of the longest semi-repetitive <span data-keyword="substring-nonempty">substring</span> of s.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">s = "52233"</span>
11 * Output: <span class="example-io">4</span>
12 * Explanation:
13 * The longest semi-repetitive substring is "5223". Picking the whole string "52233" has two adjacent same digit pairs 22 and 33, but at most one is allowed.
14 * </div>
15 * <strong class="example">Example 2:
16 * <div class="example-block">
17 * Input: <span class="example-io">s = "5494"</span>
18 * Output: <span class="example-io">4</span>
19 * Explanation:
20 * s is a semi-repetitive string.
21 * </div>
22 * <strong class="example">Example 3:
23 * <div class="example-block">
24 * Input: <span class="example-io">s = "1111111"</span>
25 * Output: <span class="example-io">2</span>
26 * Explanation:
27 * The longest semi-repetitive substring is "11". Picking the substring "111" has two adjacent same digit pairs, but at most one is allowed.
28 * </div>
29 *  
30 * Constraints:
31 * 
32 * 	1 <= s.length <= 50
33 * 	'0' <= s[i] <= '9'
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/find-the-longest-semi-repetitive-substring/
39// discuss: https://leetcode.com/problems/find-the-longest-semi-repetitive-substring/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn longest_semi_repetitive_substring(s: String) -> i32 {
45        0
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_2730() {
57    }
58}
59


Back
© 2025 bowen.ge All Rights Reserved.