Leetcode # 2800. Shortest String That Contains Three Strings
Solution
Time Complexity: O()
Space Complexity: O()
(The input and output generally do not count towards the space complexity.)
class Solution:
def minimumString(self, a: str, b: str, c: str) -> str:
_key = lambda s: (len(s), s)
def mininum_string(str1, str2):
long = str1 if len(str1) >= len(str2) else str2
short = str2 if len(str1) >= len(str2) else str1
fromend = None
for i in range(len(long)):
if fromend:
break
for j in range(len(long) - i):
if long[i + j] != short[j]:
break
if j == len(short) - 1:
return long
if i + j == len(long) - 1:
fromend = long + short[j + 1:]
for i in range(len(long) - 1, -1, -1):
for j in range(i + 1):
if long[i - j] != short[-j - 1]:
break
if i - j == 0:
fromstart = short[:len(short) - j - 1] + long
output = min(fromstart, fromend, key=_key) if fromend else fromstart
return output
output = fromend if fromend else min (str1 + str2, str2 + str1)
return output
abcs = set([mininum_string(mininum_string(s1, s2), s3) for s1, s2, s3 in [[a, b, c], [b, c, a], [a, c, b]]])
return min(abcs, key=_key)
Last Updated on 2023/08/16 by A1go