Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Brute Force ^-) solution in Clear category for Long Repeat by avko
def long_repeat(line):
"""
length the longest substring that consists of the same char
"""
# Check if a line is empty or all the chars in the line are unique
chars = set(line)
if len(chars) == len(line):
return 1 if line else 0
# If there are repeating chars in the line...
result = 1 # ... we can guarantee the shortest length of the substring
for char in chars:
for n in range (result, len(line) - len(chars) + 2):
if char * n not in line: #find the longest substring
break #of the same char
result = n # substrings of other chars shouldn't be shorter
# than already found
return result
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert long_repeat('sdsffffse') == 4, "First"
assert long_repeat('ddvvrwwwrggg') == 3, "Second"
assert long_repeat('abababaab') == 2, "Third"
assert long_repeat('') == 0, "Empty"
print('"Run" is good. How is "Check"?')
Feb. 1, 2019
Comments: