Skip to content

Instantly share code, notes, and snippets.

@AashishNandakumar
Last active September 11, 2024 09:50
Show Gist options
  • Save AashishNandakumar/cbbf34968ae4fc62823a9f22366dc3df to your computer and use it in GitHub Desktop.
Save AashishNandakumar/cbbf34968ae4fc62823a9f22366dc3df to your computer and use it in GitHub Desktop.
September 11th 2024 Questions
t = "#" + input()
n = len(t)
longest_valid = ""
for i in range(2, n):
left_substring = t[1 : i + 1]
right_substring = t[i : i + len(left_substring)]
# print(f"left sub: {left_substring}; right sub: {right_substring}")
if left_substring == right_substring:
if len(left_substring) > len(longest_valid):
longest_valid = left_substring
else:
if len(longest_valid) != 0:
print("YES")
print(longest_valid)
else:
print("NO")
t = input()
n = len(t)
longest_valid = ""
for i in range(1, n):
substring = t[: i + 1]
if len(substring) <= n // 2:
continue
if t.startswith(substring) and t.endswith(substring):
# print(t[i + 1 : i + len(substring) + 1])
if (
substring != t[i + 1 : i + len(substring) + 1]
and len(substring) > len(longest_valid)
and len(substring) < len(t)
):
longest_valid = substring
else:
if len(longest_valid) != 0:
print("YES")
print(longest_valid)
else:
print("NO")
t = input()
n = len(t)
longest_valid = ""
for i in range(1, n):
# get the substring from the start to index i (including)
substring = t[: i + 1]
# eliminating conditions
if (
# if the length of the substring is less than half of the message string, then it can't be the OG string
len(substring) <= n // 2
# the length of substring should not exceed the message string the it can't be the OG string
or len(substring) >= len(t)
# for case abcabc - appending one message after the another is not an error
or substring == t[i + 1 : i + len(substring) + 1]
):
continue
# acceptance conditions
if (
# the message string should start and end with the substring
t.startswith(substring)
and t.endswith(substring)
# update the longest_valid if the current length of the substring is greater
and len(substring) > len(longest_valid)
):
longest_valid = substring
else:
if len(longest_valid) != 0:
print("YES")
print(longest_valid)
else:
print("NO")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment