国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

LeetCode 2716. Minimize String Length

2023-06-04 12:14 作者:您是打尖兒還是住店呢  | 我要投稿

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

  • Choose an index?i?in the string, and let?c?be the character in position?i.?Delete?the?closest occurrence?of?c?to the?left?of?i?(if any) and the?closest occurrence?of?c?to the?right?of?i?(if any).

Your task is to?minimize?the length of?s?by 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

  • s?contains 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.


LeetCode 2716. Minimize String Length的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
江油市| 晋江市| 巴楚县| 凤翔县| 金塔县| 乌鲁木齐市| 湾仔区| 德令哈市| 安康市| 丁青县| 筠连县| 靖远县| 美姑县| 凤山县| 荥阳市| 南宫市| 武功县| 鹤壁市| 锡林浩特市| 汝州市| 建昌县| 盐源县| 清流县| 手游| 珠海市| 原平市| 彝良县| 响水县| 台南市| 陆川县| 康保县| 平舆县| 民勤县| 扎赉特旗| 全州县| 西藏| 定陶县| 陆良县| 丰镇市| 泗阳县| 兖州市|