Files
freeCodeCamp/curriculum/challenges/english/blocks/daily-coding-challenges-python/69cfca90e8a0a6d4d6871c54.md
T

1.9 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69cfca90e8a0a6d4d6871c54 Challenge 270: Longest Common Substring 29 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".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_longest_substring("abracadabra"), "abra")`)
}})

get_longest_substring("hello world hello") should return "hello".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_longest_substring("hello world hello"), "hello")`)
}})

get_longest_substring("mississippi") should return "issi".

({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".

({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".

({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--

def get_longest_substring(s):

    return s

--solutions--

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