力扣:1544:整理字符串
This commit is contained in:
parent
ec3d632252
commit
bf9ea9343d
@ -0,0 +1,86 @@
|
||||
//给你一个由大小写英文字母组成的字符串 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 只包含小写和大写英文字母
|
||||
//
|
||||
// Related Topics 栈 字符串
|
||||
// 👍 21 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
//1544:整理字符串
|
||||
public class MakeTheStringGreat {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new MakeTheStringGreat().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public String makeGood(String s) {
|
||||
Stack<Character> stack = new Stack<>();
|
||||
for (char ch : s.toCharArray()) {
|
||||
if (!stack.isEmpty() && (stack.peek() - ch == 32 || stack.peek() - ch == -32)) {
|
||||
stack.pop();
|
||||
} else {
|
||||
stack.push(ch);
|
||||
}
|
||||
}
|
||||
s = "";
|
||||
while (!stack.isEmpty()) {
|
||||
s = stack.pop() + s;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
<p>给你一个由大小写英文字母组成的字符串 <code>s</code> 。</p>
|
||||
|
||||
<p>一个整理好的字符串中,两个相邻字符 <code>s[i]</code> 和 <code>s[i+1]</code>,其中 <code>0<= i <= s.length-2</code> ,要满足如下条件:</p>
|
||||
|
||||
<ul>
|
||||
<li>若 <code>s[i]</code> 是小写字符,则 <code>s[i+1]</code> 不可以是相同的大写字符。</li>
|
||||
<li>若 <code>s[i]</code> 是大写字符,则 <code>s[i+1]</code> 不可以是相同的小写字符。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你将字符串整理好,每次你都可以从字符串中选出满足上述条件的 <strong>两个相邻</strong> 字符并删除,直到字符串整理好为止。</p>
|
||||
|
||||
<p>请返回整理好的 <strong>字符串</strong> 。题目保证在给出的约束条件下,测试样例对应的答案是唯一的。</p>
|
||||
|
||||
<p><strong>注意:</strong>空字符串也属于整理好的字符串,尽管其中没有任何字符。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "leEeetcode"
|
||||
<strong>输出:</strong>"leetcode"
|
||||
<strong>解释:</strong>无论你第一次选的是 i = 1 还是 i = 2,都会使 "leEeetcode" 缩减为 "leetcode" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abBAcC"
|
||||
<strong>输出:</strong>""
|
||||
<strong>解释:</strong>存在多种不同情况,但所有的情况都会导致相同的结果。例如:
|
||||
"abBAcC" --> "aAcC" --> "cC" --> ""
|
||||
"abBAcC" --> "abBA" --> "aA" --> ""
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "s"
|
||||
<strong>输出:</strong>"s"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> 只包含小写和大写英文字母</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>栈</li><li>字符串</li></div></div>\n<div><li>👍 21</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user