-
Notifications
You must be signed in to change notification settings - Fork 0
/
HRPSA0032.py
41 lines (39 loc) · 949 Bytes
/
HRPSA0032.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
#Solution for "String Similarity"
#Link: https://www.hackerrank.com/challenges/string-similarity/problem?isFullScreen=true
#Problem Solving-> Algorithms-> Data Structures
import math
import os
import random
import re
import sys
t = int(input())
for t_itr in range(t):
s = input()
char_array = list(s)
n = len(s)
c = len(s)
z = [n]
R = 0
L = 0
for i in range(1, n):
if (i > R):
L = R = i
while R < n and char_array[R-L] == char_array[R]:
R += 1
z.append(R-L)
R -= 1
c += z[i]
else:
k = i-L
if z[k] < R-i+1:
z.append(z[k])
c += z[i]
else:
L = i
while R < n and char_array[R-L] == char_array[R]:
R += 1
z.append(R-L)
c += z[i]
R -= 1
s = c
print(s)