首页 > 其他分享 >[LeetCode] 2390. Removing Stars From a String

[LeetCode] 2390. Removing Stars From a String

时间:2023-04-11 12:55:38浏览次数:50  
标签:star string 星号 Removing stack stars Stars 移除 LeetCode

You are given a string s, which contains stars *.

In one operation, you can:

  • Choose a star in s.
  • Remove the closest non-star character to its left, as well as remove the star itself.

Return the string after all stars have been removed.

Note:

  • The input will be generated such that the operation is always possible.
  • It can be shown that the resulting string will always be unique.

Example 1:

Input: s = "leet**cod*e"
Output: "lecoe"
Explanation: Performing the removals from left to right:
- The closest character to the 1st star is 't' in "leet**cod*e". s becomes "lee*cod*e".
- The closest character to the 2nd star is 'e' in "lee*cod*e". s becomes "lecod*e".
- The closest character to the 3rd star is 'd' in "lecod*e". s becomes "lecoe".
There are no more stars, so we return "lecoe".

Example 2:

Input: s = "erase*****"
Output: ""
Explanation: The entire string is removed, so we return an empty string.

Constraints:

  • 1 <= s.length <= 105
  • s consists of lowercase English letters and stars *.
  • The operation above can be performed on s.

从字符串中移除星号。

给你一个包含若干星号 * 的字符串 s 。

在一步操作中,你可以:

选中 s 中的一个星号。
移除星号 左侧 最近的那个 非星号 字符,并移除该星号自身。
返回移除 所有 星号之后的字符串。

注意:

生成的输入保证总是可以执行题面中描述的操作。
可以证明结果字符串是唯一的。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/removing-stars-from-a-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是 stack。如果遇到字母则入栈;遇到星号的时候,如果栈内有字母则弹出,没有字母则可以直接丢弃星号。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public String removeStars(String s) {
 3         Deque<Character> stack = new ArrayDeque<>();
 4         for (char c : s.toCharArray()) {
 5             if (c == '*') {
 6                 if (!stack.isEmpty()) {
 7                     stack.pollLast();
 8                 }
 9             } else {
10                 stack.offerLast(c);
11             }
12         }
13         StringBuilder sb = new StringBuilder();
14         while (!stack.isEmpty()) {
15             sb.append(stack.pollFirst());
16         }
17         return sb.toString();
18     }
19 }

 

LeetCode 题目总结

标签:star,string,星号,Removing,stack,stars,Stars,移除,LeetCode
From: https://www.cnblogs.com/cnoodle/p/17305873.html

相关文章

  • 差分数组-leetcode1094
    车上最初有 capacity 个空座位。车 只能 向一个方向行驶(也就是说,不允许掉头或改变方向)给定整数 capacity 和一个数组 trips , trip[i]=[numPassengersi,fromi,toi] 表示第 i 次旅行有 numPassengersi 乘客,接他们和放他们的位置分别是 fromi 和 toi 。这些位......
  • leetcode_打卡1
    leetcode_打卡1题目:1768.交替合并字符串解答:思路:模拟即可,字符串的提取:a.charAt(i)classSolution{publicStringmergeAlternately(Stringword1,Stringword2){Stringresult="";intm=word1.length();intn=word2.length();......
  • #yyds干货盘点# LeetCode程序员面试金典:合并两个有序链表
    题目:将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。  示例1:输入:l1=[1,2,4],l2=[1,3,4]输出:[1,1,2,3,4,4]示例2:输入:l1=[],l2=[]输出:[]示例3:输入:l1=[],l2=[0]输出:[0]代码实现:classSolution{publicLis......
  • #yyds干货盘点# LeetCode面试题:编辑距离
    1.简述:给你两个单词 word1和 word2,请返回将 word1 转换成 word2所使用的最少操作数 。你可以对一个单词进行如下三种操作:插入一个字符删除一个字符替换一个字符 示例 1:输入:word1="horse",word2="ros"输出:3解释:horse->rorse(将'h'替换为'r')rorse->rose(......
  • leetcode 178
    分数排名selects1.score,count(distincts2.score)as`rank`fromScoresass1,Scoresass2wheres1.score<=s2.scoregroupbys1.idorderbys1.scoredesc mysql8.0下新增窗口函数dense_rank()selectscore,dense_rank()over(orderbyscoredesc)`ra......
  • leetcode 177
    第N高的薪水 CREATEFUNCTIONgetNthHighestSalary(NINT)RETURNSINTBEGINdeclareTintdefault0;SETT=N-1;RETURN(#WriteyourMySQLquerystatementbelow.selectifnull((selectdistinctsalaryfromEmployeeorderbysalarydesclimit......
  • LeetCode 959. 有斜杠划分区域
    题目: https://leetcode.cn/problems/regions-cut-by-slashes/description/题解(参考了讨论区):将初始N*N的网格看做4*N*N的三角形集合,根据输入合并对应的三角形。   C#实现 publicclassSolution{publicintRegionsBySlashes(string[]grid){Uni......
  • LeetCode 530.二叉搜索树的最小绝对值差
    1.题目:给你一个二叉搜索树的根节点root,返回树中任意两不同节点值之间的最小差值。差值是一个正数,其数值等于两值之差的绝对值。示例1:输入:root=[4,2,6,1,3]输出:1来源:力扣(LeetCode)链接:https://leetcode.cn/problems/minimum-absolute-difference-in-bst著作权归领扣网络所......
  • LeetCode习题——x 的平方根(二分查找)
    ###x的平方根力扣链接:[x的平方根](https://leetcode.cn/problems/sqrtx/)####题目>给你一个非负整数x,计算并返回x的算术平方根。>>由于返回类型是整数,结果只保留整数部分,小数部分将被舍去。>>注意:不允许使用任何内置指数函数和算符,例如pow(x,0.5)或者x*......
  • LeetCode 98.验证二叉搜索树
    1.题目:给你一个二叉树的根节点root,判断其是否是一个有效的二叉搜索树。有效二叉搜索树定义如下:节点的左子树只包含小于当前节点的数。节点的右子树只包含大于当前节点的数。所有左子树和右子树自身必须也是二叉搜索树。示例1:输入:root=[2,1,3]输出:true来源:力扣(LeetCode......