Skip to content

Commit e05cafc

Browse files
authored
longest non-overlapping repeating substring
Initial File
1 parent 28fc848 commit e05cafc

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def longestRepeatedSubstring(str):
2+
n = len(str)
3+
LRS = [[0 for x in range(n + 1)]for y in range(n + 1)]
4+
5+
res = ""
6+
res_length = 0
7+
index = 0
8+
for i in range(1, n + 1):
9+
for j in range(i + 1, n + 1):
10+
if (str[i - 1] == str[j - 1] and LRS[i - 1][j - 1] < (j - i)):
11+
LRS[i][j] = LRS[i - 1][j - 1] + 1
12+
13+
if (LRS[i][j] > res_length):
14+
res_length = LRS[i][j]
15+
index = max(i, index)
16+
else:
17+
LRS[i][j] = 0
18+
if (res_length > 0):
19+
for i in range(index - res_length + 1,
20+
index + 1):
21+
res = res + str[i - 1]
22+
23+
return res
24+
25+
26+
if __name__ == "__main__":
27+
str = "banana"
28+
print(longestRepeatedSubstring(str))
29+

0 commit comments

Comments
 (0)