zoukankan      html  css  js  c++  java
  • 前缀和-1915. 最美子字符串的数目

    2021-07-04 20:56:40

    问题描述:

    如果某个字符串中 至多一个 字母出现奇数次,则称其为最美字符串。

    例如,"ccjjc" 和 "abab" 都是最美字符串,但 "ab" 不是。
    给你一个字符串 word ,该字符串由前十个小写英文字母组成('a' 到 'j')。请你返回 word 中 最美非空子字符串 的数目。如果同样的子字符串在 word 中出现多次,那么应当对 每次出现 分别计数。

    子字符串 是字符串中的一个连续字符序列。

    示例 1:

    输入:word = "aba"
    输出:4
    解释:4 个最美子字符串如下所示:
    - "aba" -> "a"
    - "aba" -> "b"
    - "aba" -> "a"
    - "aba" -> "aba"
    示例 2:

    输入:word = "aabb"
    输出:9
    解释:9 个最美子字符串如下所示:
    - "aabb" -> "a"
    - "aabb" -> "aa"
    - "aabb" -> "aab"
    - "aabb" -> "aabb"
    - "aabb" -> "a"
    - "aabb" -> "abb"
    - "aabb" -> "b"
    - "aabb" -> "bb"
    - "aabb" -> "b"
    示例 3:

    输入:word = "he"
    输出:2
    解释:2 个最美子字符串如下所示:
    - "he" -> "h"
    - "he" -> "e"
     

    提示:

    1 <= word.length <= 105
    word 由从 'a' 到 'j' 的小写英文字母组成

    问题求解:

    使用0,1去表征奇偶性,利用前缀和解决。

    class Solution:
        def wonderfulSubstrings(self, word: str) -> int:
            res = 0
            n = len(word)
            freq = [0] * (1 << 10)
            freq[0] += 1
            state = 0
            for i in range(n):
                offset = ord(word[i]) - ord("a")
                state = state ^ (1 << offset)
                res += freq[state]
                for j in range(10):
                    prev = state ^ (1 << j)
                    res += freq[prev]
                freq[state] += 1
            return res
    

      

    相似题型:

    1542. 找出最长的超赞子字符串

    问题描述:

    给你一个字符串 s 。请返回 s 中最长的 超赞子字符串 的长度。

    「超赞子字符串」需满足满足下述两个条件:

    该字符串是 s 的一个非空子字符串
    进行任意次数的字符交换后,该字符串可以变成一个回文字符串
     

    示例 1:

    输入:s = "3242415"
    输出:5
    解释:"24241" 是最长的超赞子字符串,交换其中的字符后,可以得到回文 "24142"
    示例 2:

    输入:s = "12345678"
    输出:1
    示例 3:

    输入:s = "213123"
    输出:6
    解释:"213123" 是最长的超赞子字符串,交换其中的字符后,可以得到回文 "231132"
    示例 4:

    输入:s = "00"
    输出:2
     

    提示:

    1 <= s.length <= 10^5
    s 仅由数字组成

    问题求解:

    class Solution:
        def longestAwesome(self, s: str) -> int:
            res = 1
            record = {}
            record[0] = -1
            n = len(s)
            state = 0
            for i in range(n):
                offset = ord(s[i]) - ord("0")
                state = state ^ (1 << offset)
                res = max(res, i - record.get(state, i))
                for j in range(10):
                    prev = state ^ (1 << j)
                    res = max(res, i - record.get(prev, i))
                if state not in record:
                    record[state] = i
            return res
    

      

  • 相关阅读:
    SentiAnalysis
    大数据索引技术 B+ tree vs LSM tree
    Regression, 回归问题
    Data Mining with R
    Why Vector Clock are Easy or Hard?
    How to know what an HRESULT code means?
    如何判断数据库表的某个列上有重复值的记录存在?
    关于SharePoint 2010里Servers in farm页面里status意义的澄清
    SharePoint Security系列 之二 CrossSite Request Forgery
    从MOSS2007升级到SharePoint2010后Report Server content types升级失败
  • 原文地址:https://www.cnblogs.com/hyserendipity/p/14969887.html
Copyright © 2011-2022 走看看