-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind the Longest Semi-Repetitive Substring.py
61 lines (43 loc) · 1.73 KB
/
Find the Longest Semi-Repetitive Substring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'''
You are given a 0-indexed string s that consists of digits from 0 to 9.
A string t is called a semi-repetitive if there is at most one consecutive
pair of the same digits inside t. For example, 0010, 002020, 0123, 2002, and
54944 are semi-repetitive while 00101022, and 1101234883 are not.
Return the length of the longest semi-repetitive substring inside s.
A substring is a contiguous non-empty sequence of characters within a string.
'''
--------------------------------------------------------------------------------------------------------------
class Solution:
def longestSemiRepetitiveSubstring(self, s: str) -> int:
def is_repet(subs):
consec_pair = 0
for i in range(len(subs)-1):
if subs[i] == subs[i+1]:
consec_pair +=1
elif subs[i] == subs[i+1] and consec_pair >= 1:
return False
if consec_pair <=1:
return True
return False
cur_len = 0
n = len(s)
for i in range(n):
for j in range(i, n+1): #here is n+1 vs n
substr = s[i:j]
if is_repet(substr):
cur_len = max(len(substr), cur_len)
return cur_len
-----------------------------------------------------------------------
#DP approach
class Solution:
def longestSemiRepetitiveSubstring(self, s: str) -> int:
res=0
tab=[1]
for i in range(1,len(s)):
if ( s[i]==s[i-1]):
tab.append(1)
else:
tab[-1]=tab[-1]+1
for j in range(1,len(tab)):
res=max(res,tab[j]+tab[j-1])
return max(res,tab[0])