当前位置: 首页> 娱乐> 明星 > 哪个公司搭建网站_新网页游戏传奇_下载百度免费_拉新推广怎么找渠道

哪个公司搭建网站_新网页游戏传奇_下载百度免费_拉新推广怎么找渠道

时间:2025/7/11 18:29:31来源:https://blog.csdn.net/sinat_41679123/article/details/145114152 浏览次数:0次
哪个公司搭建网站_新网页游戏传奇_下载百度免费_拉新推广怎么找渠道

Description

You are given a string s.

You can perform the following process on s any number of times:

  • Choose an index i in the string such that there is at least one character to the left of index i that is equal to s[i], and at least one character to the right that is also equal to s[i].
  • Delete the closest character to the left of index i that is equal to s[i].
  • Delete the closest character to the right of index i that is equal to s[i].

Return the minimum length of the final string s that you can achieve.

Example 1:

Input: s = "abaacbcbb"Output: 5Explanation:
We do the following operations:Choose index 2, then remove the characters at indices 0 and 3. The resulting string is s = "bacbcbb".
Choose index 3, then remove the characters at indices 0 and 5. The resulting string is s = "acbcb".
Example 2:Input: s = "aa"

Output: 2

Explanation:
We cannot perform any operations, so we return the length of the original string.

Constraints:

1 <= s.length <= 2 * 10^5
s consists only of lowercase English letters.

Solution

Because we only need to get the minimum length of the string, so we don’t actually need to decide which character we need to delete. Just count the frequency of all characters, and then for anything above 2, if it’s an odd number, we have 1 (keep deleting 2s until it’s below 3), for even numbers, we have 2 (keep deleting 2s until it’s below 3). Add those numbers up and we have our result.

Update: for frequencies below 3, it’s the same: for 1 we add 1, for 2 we add 2.
Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( n ) ≈ o ( 1 ) o(n) \approx o(1) o(n)o(1), because we have at most 26 lowercase characters.

Code

class Solution:def minimumLength(self, s: str) -> int:ch_cnt = collections.Counter(s)res = 0for ch, cnt in ch_cnt.items():res += (1 if cnt & 1 == 1 else 2)return res
关键字:哪个公司搭建网站_新网页游戏传奇_下载百度免费_拉新推广怎么找渠道

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: