Created
July 28, 2019 23:32
-
-
Save brokensandals/5aa7bffa693c70d80868f023c02b74cb to your computer and use it in GitHub Desktop.
A python solution to HackerRank's "Common Child" problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import os | |
# Complete the commonChild function below. | |
def commonChild(s1, s2): | |
maxAt = {} | |
for i1 in range(len(s1)): | |
maxForI1 = 0 | |
for i2 in range(len(s2)): | |
potentialSum = maxForI1 + 1 | |
# You might be tempted to use the max() function to simplify the next three lines, | |
# but that makes the solution so much slower that several of the test cases fail. | |
other = maxAt.get(i2, 0) | |
if other > maxForI1: | |
maxForI1 = other | |
if s1[i1] == s2[i2]: | |
maxAt[i2] = potentialSum | |
return max(maxAt.values(), default=0) | |
if __name__ == '__main__': | |
fptr = open(os.environ['OUTPUT_PATH'], 'w') | |
s1 = input() | |
s2 = input() | |
result = commonChild(s1, s2) | |
fptr.write(str(result) + '\n') | |
fptr.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you Explain this?