Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

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