加载中...
1544-整理字符串(Make The String Great)
发表于:2021-12-03 | 分类: 简单
字数统计: 317 | 阅读时长: 1分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/make-the-string-great

英文原文

Given a string s of lower and upper case English letters.

A good string is a string which doesn't have two adjacent characters s[i] and s[i + 1] where:

  • 0 <= i <= s.length - 2
  • s[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case or vice-versa.

To make the string good, you can choose two adjacent characters that make the string bad and remove them. You can keep doing this until the string becomes good.

Return the string after making it good. The answer is guaranteed to be unique under the given constraints.

Notice that an empty string is also good.

 

Example 1:

Input: s = "leEeetcode"
Output: "leetcode"
Explanation: In the first step, either you choose i = 1 or i = 2, both will result "leEeetcode" to be reduced to "leetcode".

Example 2:

Input: s = "abBAcC"
Output: ""
Explanation: We have many possible scenarios, and all lead to the same answer. For example:
"abBAcC" --> "aAcC" --> "cC" --> ""
"abBAcC" --> "abBA" --> "aA" --> ""

Example 3:

Input: s = "s"
Output: "s"

 

Constraints:

  • 1 <= s.length <= 100
  • s contains only lower and upper case English letters.

中文题目

给你一个由大小写英文字母组成的字符串 s

一个整理好的字符串中,两个相邻字符 s[i]s[i+1],其中 0<= i <= s.length-2 ,要满足如下条件:

  • s[i] 是小写字符,则 s[i+1] 不可以是相同的大写字符。
  • s[i] 是大写字符,则 s[i+1] 不可以是相同的小写字符。

请你将字符串整理好,每次你都可以从字符串中选出满足上述条件的 两个相邻 字符并删除,直到字符串整理好为止。

请返回整理好的 字符串 。题目保证在给出的约束条件下,测试样例对应的答案是唯一的。

注意:空字符串也属于整理好的字符串,尽管其中没有任何字符。

 

示例 1:

输入:s = "leEeetcode"
输出:"leetcode"
解释:无论你第一次选的是 i = 1 还是 i = 2,都会使 "leEeetcode" 缩减为 "leetcode" 。

示例 2:

输入:s = "abBAcC"
输出:""
解释:存在多种不同情况,但所有的情况都会导致相同的结果。例如:
"abBAcC" --> "aAcC" --> "cC" --> ""
"abBAcC" --> "abBA" --> "aA" --> ""

示例 3:

输入:s = "s"
输出:"s"

 

提示:

  • 1 <= s.length <= 100
  • s 只包含小写和大写英文字母

通过代码

高赞题解

图解每日一练.jpg


🧠 解题思路

分析题意之后,可以得出以下结论:

  1. 字符要做比较,所以之前的字符应该被存储下来,这里我们会用到栈。
  2. 遍历字符,若栈顶和当前字符正好大小写都具备,则弹出栈顶抵消,否则当前字符入栈。

🎨 图解演示

<1.jpg,2.jpg,3.jpg,4.jpg,5.jpg,6.jpg>


🍭 示例代码

[]
var makeGood = function(s) { let res = []; for(let i of s){ if( res.length && res[res.length - 1] !== i && res[res.length - 1].toUpperCase() === i.toUpperCase() ){ res.pop(); }else{ res.push(i); } } return res.join(""); };

转身挥手

嘿,少年,做图不易,留下个赞或评论再走吧!谢啦~ 💐

差点忘了,祝你牛年大吉 🐮 ,AC 和 Offer 📑 多多益善~

⛲⛲⛲ 期待下次再见~

统计信息

通过次数 提交次数 AC比率
20712 37734 54.9%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
1558-得到目标数组的最少函数调用次数(Minimum Numbers of Function Calls to Make Target Array)
下一篇:
1545-找出第 N 个二进制字符串中的第 K 位(Find Kth Bit in Nth Binary String)
本文目录
本文目录