首页 > 热点 > > 正文

LeetCode 2716. Minimize String Length

时间:2023-06-04 14:27:50 来源:哔哩哔哩

Given a 0-indexed string s, repeatedly perform the following operation any number of times:


(资料图片仅供参考)

Choose an index iin the string, and let cbe the character in position i. Delete the closest occurrence of cto the left of i(if any) and the closest occurrence of cto the right of i(if any).

Your task is to minimize the length of sby performing the above operation any number of times.

Return an integer denoting the length of the minimized string.

Example 1:

Input: s = "aaabc"Output: 3

Explanation: In this example, s is "aaabc". We can start by selecting the character 'a' at index 1. We then remove the closest 'a' to the left of index 1, which is at index 0, and the closest 'a' to the right of index 1, which is at index 2. After this operation, the string becomes "abc". Any further operation we perform on the string will leave it unchanged. Therefore, the length of the minimized string is 3.

Example 2:

Input: s = "cbbd"Output: 3

Explanation: 

For this we can start with character 'b' at index 1. There is no occurrence of 'b' to the left of index 1, but there is one to the right at index 2, so we delete the 'b' at index 2. The string becomes "cbd" and further operations will leave it unchanged. Hence, the minimized length is 3. 

Example 3:

Input: s = "dddaaa"Output: 2

Explanation: For this, we can start with the character 'd' at index 1. The closest occurrence of a 'd' to its left is at index 0, and the closest occurrence of a 'd' to its right is at index 2. We delete both index 0 and 2, so the string becomes "daaa". In the new string, we can select the character 'a' at index 2. The closest occurrence of an 'a' to its left is at index 1, and the closest occurrence of an 'a' to its right is at index 3. We delete both of them, and the string becomes "da". We cannot minimize this further, so the minimized length is 2.

用hashset更快;

Constraints:

1 <= s.length <= 100

scontains only lowercase English letters

Runtime: 5 ms, faster than 100.00% of Java online submissions for Minimize String Length.

Memory Usage: 43.8 MB, less than 25.00% of Java online submissions for Minimize String Length.

关键词:
x 广告
共同抗疫!郑州71岁党员志愿者每天爬6栋楼,提醒800多户居民做核酸

1月4日,郑州市新冠肺炎疫情防控指挥部办公室发布通告对部分区域实施管控措施。这是近半年来,新冠疫情第3次造访郑州。截止至1月10日24时,

安阳疫情最新消息|1月11日8时至12日8时安阳市新增本土确诊病例39例,累计123例

安阳疫情最新消息2022年1月11日8时至12日8时安阳市新型冠状病毒肺炎疫情最新情况1月11日8时至12日8时,全市新增本土确诊病例39例(汤阴县38

迅速高效!“河南抗疫互助通道”网上网下联动 获群众点赞

核酸检测24小时了还没查到结果,怎么办?现在高速下站口的管控政策到底是什么样子的?从洛阳老城区自驾走高速回郑州,收费站还劝返吗?苏八

信阳市疫情防控工作视频调度会召开 陈志伟出席会议

1月11日,市疫情防控工作视频调度会召开。代市长陈志伟出席会议并讲话。听取有关疫情防控重点工作和各县区(管理区、开发区)疫情防控工作

全国最新疫情消息:11日新增本土确诊病例166例 天津市今日启动第二轮全员核酸检测

11日0—24时,31个省(自治区、直辖市)和新疆生产建设兵团报告新增新冠肺炎本土确诊病例166例(河南118例,其中安阳市65例、许昌市41例、

1月12日天津市放假半天 开启第二轮全员核酸检测

1月12日,天津市人民政府发布重要通告,为做好天津市第二轮全员核酸检测工作,全市机关和企事业单位于1月12日(星期三)下午放假半天。广大

x 广告

Copyright ©  2015-2023 华夏评测网版权所有  备案号:琼ICP备2022009675号-37   联系邮箱:435 227 67@qq.com