zoukankan      html  css  js  c++  java
  • [Swift]LeetCode1234. 替换子串得到平衡字符串 | Replace the Substring for Balanced String

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(let_us_code)
    ➤博主域名:https://www.zengqiang.org
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/11712816.html
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    You are given a string containing only 4 kinds of characters 'Q', 'W', 'E' and 'R'.

    A string is said to be balanced if each of its characters appears n/4 times where n is the length of the string.

    Return the minimum length of the substring that can be replaced with any other string of the same length to make the original string s balanced.

    Return 0 if the string is already balanced.

    Example 1:

    Input: s = "QWER"
    Output: 0
    Explanation: s is already balanced.
    Example 2:

    Input: s = "QQWE"
    Output: 1
    Explanation: We need to replace a 'Q' to 'R', so that "RQWE" (or "QRWE") is balanced.
    Example 3:

    Input: s = "QQQW"
    Output: 2
    Explanation: We can replace the first "QQ" to "ER".
    Example 4:

    Input: s = "QQQQ"
    Output: 3
    Explanation: We can replace the last 3 'Q' to make s = "QWER".

    Constraints:

    1 <= s.length <= 10^5
    s.length is a multiple of 4
    s contains only 'Q', 'W', 'E' and 'R'.


    有一个只含有 'Q', 'W', 'E', 'R' 四种字符,且长度为 n 的字符串。

    假如在该字符串中,这四个字符都恰好出现 n/4 次,那么它就是一个「平衡字符串」。 

    给你一个这样的字符串 s,请通过「替换子串」的方式,使原字符串 s 变成一个「平衡字符串」。

    你可以用和「待替换子串」长度相同的 任何 其他字符串来完成替换。

    请返回待替换子串的最小可能长度。

    如果原字符串自身就是一个平衡字符串,则返回 0。

    示例 1:

    输入:s = "QWER"
    输出:0
    解释:s 已经是平衡的了。
    示例 2:

    输入:s = "QQWE"
    输出:1
    解释:我们需要把一个 'Q' 替换成 'R',这样得到的 "RQWE" (或 "QRWE") 是平衡的。
    示例 3:

    输入:s = "QQQW"
    输出:2
    解释:我们可以把前面的 "QQ" 替换成 "ER"。
    示例 4:

    输入:s = "QQQQ"
    输出:3
    解释:我们可以替换后 3 个 'Q',使 s = "QWER"。

    提示:

    1 <= s.length <= 10^5
    s.length 是 4 的倍数
    s 中只含有 'Q', 'W', 'E', 'R' 四种字符

  • 相关阅读:
    2020-06-20 助教一周小结(第十七周)
    2020-06-14 助教一周小结(第十六周)
    2020-06-7助教一周小结(第14周)
    2020本科校招-从小白到拿到30k offer的学习经历
    2020-05-24助教一周小结(第13周)
    2020-05-17 助教一周小结(第十二周)
    2020-05-10 助教一周小结(第十一周)
    2020-05-04 助教一周小结(第十周)
    2020-04-26 助教一周小结(第九周)
    Ngnix搭建静态网页和安装wordpress
  • 原文地址:https://www.cnblogs.com/strengthen/p/11712816.html
Copyright © 2011-2022 走看看