--- id: 69cfca90e8a0a6d4d6871c54 title: "Challenge 270: Longest Common Substring" challengeType: 29 dashedName: challenge-270 --- # --description-- Given a string, return the longest substring that appears more than once. - The substrings can overlap. # --hints-- `get_longest_substring("abracadabra")` should return `"abra"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(get_longest_substring("abracadabra"), "abra")`) }}) ``` `get_longest_substring("hello world hello")` should return `"hello"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(get_longest_substring("hello world hello"), "hello")`) }}) ``` `get_longest_substring("mississippi")` should return `"issi"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(get_longest_substring("mississippi"), "issi")`) }}) ``` `get_longest_substring("ha ha ha ha ha ha ha")` should return `"ha ha ha ha ha ha"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(get_longest_substring("ha ha ha ha ha ha ha"), "ha ha ha ha ha ha")`) }}) ``` `get_longest_substring("the quick brown fox jumped over the lazy dog that the quick brown fox jumped over")` should return `"the quick brown fox jumped over"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(get_longest_substring("the quick brown fox jumped over the lazy dog that the quick brown fox jumped over"), "the quick brown fox jumped over")`) }}) ``` # --seed-- ## --seed-contents-- ```py def get_longest_substring(s): return s ``` # --solutions-- ```py def get_longest_substring(s): longest = '' for length in range(len(s) - 1, 0, -1): for i in range(len(s) - length + 1): sub = s[i:i + length] if s.find(sub) != s.rfind(sub): return sub return longest ```