-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstructTheLongestNewString.py
51 lines (40 loc) · 1.17 KB
/
ConstructTheLongestNewString.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
# You are given three integers x, y, and z.
# You have x strings equal to "AA", y strings equal to "BB", and z strings equal to "AB". You want to choose some (possibly all or none) of these strings and concatenate them in some order to form a new string. This new string must not contain "AAA" or "BBB" as a substring.
# Return the maximum possible length of the new string.
# A substring is a contiguous non-empty sequence of characters within a string.
# naive solution
def longestString1(x, y, z):
newStringLen = 0
if x <= y + 1:
newStringLen += x * 2
else:
newStringLen += (y + 1) * 2
if y <= x:
newStringLen += y * 2
else:
newStringLen += (x + 1) * 2
newStringLen += z * 2
return newStringLen
# more clever solution: faster runtime
def longestString(x, y, z):
newStringLen = z * 2
if x < y:
newStringLen += (x * 4) + 2
elif x > y:
newStringLen += (y * 4) + 2
else:
newStringLen += x * 4
return newStringLen
# Test cases
x = 2
y = 5
z = 1
print(longestString(x, y, z))
x = 3
y = 2
z = 2
print(longestString(x, y, z))
x = 1
y = 39
z = 14
print(longestString(x, y, z))