【Python】字符串最优分割

给定一个有小写字母组成的字符串S,将其分割成尽量多的子串,并保证每个字母最多只在其中一个子串中出现。请返回由一个或多个整数表示的分割后各个子串的长度。如:‘ababbacadefgdehijhklij’=  ‘ababbaca‘ + ’defgde‘+ ’hijhklij‘。

#str1 = 'ababbacadefgdehijhklij'  
str1 = input()
def str_count(str_n):
    count = 0
    while(str_n != ''):
        count = count + 1
        max_num = 0 
        len1 = len(str_n)                      
        start = str_n[0]  
        '''         
        for i in range(len1):
            if start == str_n[i]:
                max_num = i
        str2 = str_n[:max_num+1]
        '''
        #获得 同一个字符之间的 字符串  如:ababbaca
        str2 = str_n[:str_n.rindex(start)+1]
        #在剩余的字符串中 依次判断是否 有在str2中的字符,如有把此之前的元素都放到str2中
        for i in range(max_num+1,len1):
            if str_n[i] in str2:
                max_num = i
        str2 = str_n[:max_num+1]                   
        str3 = str_n[max_num+1:]          
        #print(str2,':',len(str2))
        print(len(str2),end=' ')
        str_n = str3


str_count(str1)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值