You are given a digit string s that consists of digits from 0 to 9.
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).
Return the length of the longest semi-repetitive substring of s.
Input: s ="52233"Output: 4Explanation:
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 isallowed.
Input: s ="1111111"Output: 2Explanation:
The longest semi-repetitive substring is"11". Picking the substring "111" has
two adjacent same digit pairs, but at most one is allowed.
We want the longest substring with at most one pair of adjacent equal digits. We can use a sliding window to keep track of the current substring and count the number of adjacent equal pairs.
classSolution {
public:int longestSemiRepetitiveSubstring(string s) {
int n = s.size(), ans =1, cnt =0, l =0;
for(int r =1; r < n; ++r) {
if(s[r] == s[r-1]) ++cnt;
while(cnt >1) {
if(s[l+1] == s[l]) --cnt;
++l;
}
ans = max(ans, r-l+1);
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
funclongestSemiRepetitiveSubstring(sstring) int {
n, ans, cnt, l:= len(s), 1, 0, 0forr:=1; r < n; r++ {
ifs[r] ==s[r-1] { cnt++ }
forcnt > 1 {
ifs[l+1] ==s[l] { cnt-- }
l++ }
ifr-l+1 > ans { ans = r-l+1 }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
publicintlongestSemiRepetitiveSubstring(String s) {
int n = s.length(), ans = 1, cnt = 0, l = 0;
for(int r = 1; r < n; ++r) {
if(s.charAt(r) == s.charAt(r-1)) ++cnt;
while(cnt > 1) {
if(s.charAt(l+1) == s.charAt(l)) --cnt;
++l;
}
ans = Math.max(ans, r-l+1);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
funlongestSemiRepetitiveSubstring(s: String): Int {
var ans = 1; var cnt = 0; var l = 0for (r in1 until s.length) {
if (s[r] == s[r-1]) cnt++while (cnt > 1) {
if (s[l+1] == s[l]) cnt-- l++ }
ans = maxOf(ans, r-l+1)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
deflongestSemiRepetitiveSubstring(self, s: str) -> int:
n = len(s)
ans =1 cnt =0 l =0for r in range(1, n):
if s[r] == s[r-1]:
cnt +=1while cnt >1:
if s[l+1] == s[l]:
cnt -=1 l +=1 ans = max(ans, r-l+1)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
impl Solution {
pubfnlongest_semi_repetitive_substring(s: String) -> i32 {
let s = s.as_bytes();
let n = s.len();
let (mut ans, mut cnt, mut l) = (1, 0, 0);
for r in1..n {
if s[r] == s[r-1] { cnt +=1; }
while cnt >1 {
if s[l+1] == s[l] { cnt -=1; }
l +=1;
}
ans = ans.max(r-l+1);
}
ans asi32 }
}