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
85 lines
1.9 KiB
Markdown
85 lines
1.9 KiB
Markdown
---
|
|
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
|
|
```
|