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

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

LeetCode 1750. Minimum Length of String After Deleting Similar E

2023-04-24 16:38 作者:您是打尖兒還是住店呢  | 我要投稿

Given a string?s?consisting only of characters?'a',?'b', and?'c'. You are asked to apply the following algorithm on the string any number of times:

  1. Pick a?non-empty?prefix from the string?s?where all the characters in the prefix are equal.

  2. Pick a?non-empty?suffix from the string?s?where all the characters in this suffix are equal.

  3. The prefix and the suffix should not intersect at any index.

  4. The characters from the prefix and suffix must be the same.

  5. Delete both the prefix and the suffix.

Return?the?minimum length?of?s?after performing the above operation any number of times (possibly zero times).

?

Example 1:

Input: s = "ca"

Output: 2

Explanation: You can't remove any characters, so the string stays as is.

Example 2:

Input: s = "cabaabac"

Output: 0

Explanation: An optimal sequence of operations is:?

- Take prefix = "c" and suffix = "c" and remove them, s = "abaaba".?

- Take prefix = "a" and suffix = "a" and remove them, s = "baab".?

- Take prefix = "b" and suffix = "b" and remove them, s = "aa".?

- Take prefix = "a" and suffix = "a" and remove them, s = "".

Example 3:

Input: s = "aabccabba"

Output: 3

Explanation: An optimal sequence of operations is:?

- Take prefix = "aa" and suffix = "a" and remove them, s = "bccabb".?

- Take prefix = "b" and suffix = "bb" and remove them, s = "cca".

?寫了3個函數(shù),

1:判斷左右兩端是否一樣,用來做循環(huán)的條件;

2:判斷左邊一樣的到哪里;

3:判斷右邊一樣的到哪里;

依次循環(huán),處理返回即可;

但是長度為1的需要特殊處理一下即可;

只是沒想到還能速度這么快。。。。

下面是代碼:

Constraints:

  • 1 <= s.length <= 105

  • s?only consists of characters?'a',?'b', and?'c'.


Runtime:?5 ms, faster than?88.12%?of?Java?online submissions for?Minimum Length of String After Deleting Similar Ends.

Memory Usage:?43.1 MB, less than?60.54%?of?Java?online submissions for?Minimum Length of String After Deleting Similar Ends.


LeetCode 1750. Minimum Length of String After Deleting Similar E的評論 (共 條)

分享到微博請遵守國家法律
巫溪县| 汶川县| 潞城市| 庄河市| 吐鲁番市| 平阳县| 韶山市| 松阳县| 渭源县| 新巴尔虎左旗| 伊川县| 阿拉尔市| 湖州市| 宜兰市| 博湖县| 龙山县| 扬中市| 上高县| 宁南县| 同心县| 甘洛县| 贺兰县| 来安县| 密山市| 大方县| 大新县| 简阳市| 鹰潭市| 潮安县| 盐边县| 许昌市| 章丘市| 呼和浩特市| 云龙县| 尼勒克县| 哈密市| 新田县| 蓝山县| 朝阳区| 宁武县| 临沂市|