AcWing
  • 首页
  • 题库
  • 题解
  • 分享
  • 问答
  • 活动
  • 应用
  • 吐槽
  • 登录/注册

5634. 删除子字符串的最大得分

作者: 作者的头像   YueGuang ,  2021-01-10 00:26:49 ,  阅读 30


1


class Solution:
    def maximumGain(self, s: str, x: int, y: int) -> int:
        res = 0
        left, right = 0, 0
        while right < len(s):
            if s[right] != 'a' and s[right] != 'b':
                string = s[left:right]
                if x < y:
                    while 'ba' in string:
                        res += y
                        string = string.replace('ba', '', 1)
                    while 'ab' in string:
                        res += x
                        string = string.replace('ab', '', 1)
                else:
                    while 'ab' in string:
                        res += x
                        string = string.replace('ab', '', 1)
                    while 'ba' in string:
                        res += y
                        string = string.replace('ba', '', 1)
                left = right + 1  
            right += 1
        string = s[left:right]
        if x < y:
            while 'ba' in string:
                res += y
                string = string.replace('ba', '', 1)
            while 'ab' in string:
                res += x
                string = string.replace('ab', '', 1)
        else:
            while 'ab' in string:
                res += x
                string = string.replace('ab', '', 1)
            while 'ba' in string:
                res += y
                string = string.replace('ba', '', 1)
        return res

1 评论


用户头像
宫水三叶   10天前     回复

牛逼 真的能过

你确定删除吗?

© 2018-2021 AcWing 版权所有  |  京ICP备17053197号-1
联系我们  |  常见问题
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息