每日leetcode4.16(扰乱字符串+无重复最长字串)

每日leetcode4.16(扰乱字符串)

87. 扰乱字符串(hard)

三维dp,四维运算,从长度为1开始向上增加

class Solution:
    def isScramble(self, s1: str, s2: str) -> bool:
        l = len(s1)
        dp = [[[0] * l for i1 in range(l)] for i2 in range(l)]
        for p in range(l):
            for i in range(l - p):
                for j in range(l - p):
                    if p == 0:
                        if s1[i] == s2[j]:
                            dp[p][i][j] = 1
                    for k in range(p):
                        if (dp[k][i][j] and dp[p - k - 1][i + k + 1][j + k + 1]) or (dp[k][i][j + p - k] and dp[p - k - 1][i + k + 1][j]):
                            dp[p][i][j] = 1
                            break
        return dp[l - 1][0][0] == 1

3 无重复字符的最长字串

本题要在O(n)内解决,滑动窗口,用一个dict记录重复字符串的位置

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        l = len(s)
        mp = {}
        i = 0
        j = 0
        ans = 0
        while j < l:
            while j < l and (mp.get(s[j]) is None or mp[s[j]] == -1):
                mp[s[j]] = j
                j += 1
            ans = max(ans, j - i)
            while j < l and i <= mp[s[j]]:
                mp[s[i]] = -1
                i += 1
        return ans
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值