重复此操作,直到字符串%s为空。
如果从%s开头开始有两个或多个连续字母
・Define n as the number of consecutive pieces.
・Display 'ns' and delete n characters from the beginning.
如果两个或多个字符不连续
・Memorize how many characters are not consecutive and display '-ns'.
・Then remove ns from the string.
举例说明。
input = 'abcaaaaaaaaaaaaaab'
output = '-3abc12a-1b'
input = 'AaAaAAAAa'
output = '-4AaAa4A-1a'
如果您熟悉该算法,了解它将会很有帮助。
1条答案
按热度按时间nlejzf6q1#
I would use a regular expression to identify the different chunks of the string, and then use the
re.sub
callback to format the chunk as specified: