Find the longest substring in alphabetical order.

Example: the longest alphabetical substring in `"asdfaaaabbbbcttavvfffffdf"` is `"aaaabbbbctt"`.

There are tests with strings up to `10 000` characters long so your code will need to be efficient.

The input will only consist of lowercase characters and will be at least one letter long.

If there are multiple solutions, return the one that appears first.

Good luck :)

Source: codewars | Difficulty: EASY
Tags: String algorithms

Fundamentals

Starter code: def longest(s):
	

Solutions:
import re
reg = re.compile('a*b*c*d*e*f*g*h*i*j*k*l*m*n*o*p*q*r*s*t*u*v*w*x*y*z*')

def longest(s):
	return max(reg.findall(s), key=len)


def longest(string):
	start = 0
	longest = string[0:1]
	length = len(string)
	for i in range(1, length):
		if string[i] < string[i - 1] or i == length - 1:
			if string[i] < string[i - 1]:
				last = string[start:i]
				start = i
			else:
				last = string[start:]
			if len(last) > len(longest):
				longest = last
	return longest


def longest(s):
	k = []
	for i in range(len(s) - 1):
		if s[i] <= s[i + 1]:
			k.append(s[i])
		else:
			k.append(s[i])
			k.append(' ')
	k += s[-1]
	return max(''.join(k).split(), key=len)


import re

def longest(s):
	regex = 'a*b*c*d*e*f*g*h*i*j*k*l*m*n*o*p*q*r*s*t*u*v*w*x*y*z*'
	group = lambda x: x.group()
	return max(map(group, re.finditer(regex, s)), key=len)


def longest(s):
	chunks = []
	for c in s:
		if chunks and chunks[-1][-1] <= c:
			chunks[-1] += c
		else:
			chunks.append(c)
	return max(chunks, key=len)


def longest(s):
	max_substring = ''
	substring = ''
	for ch in s:
		if substring == '' or ord(ch) >= ord(substring[-1]):
			substring += ch
		elif ord(ch) < ord(substring[-1]):
			substring = ''.join(ch)
		if len(substring) > len(max_substring):
			max_substring = substring
	return max_substring


longest = lambda s: max(''.join((c + ' ' if c > d else c for (c, d) in zip(s, s[1:] + ' '))).split(), key=len)


def longest(s):
	result = []
	ordx = 0
	for i in s:
		if ord(i) >= ordx:
			ordx = ord(i)
			result.append(i)
		else:
			result.append('|')
			result.append(i)
			ordx = ord(i)
	return max(''.join(result).split('|'), key=len)