Repeatedly remove all adjacent, repeated characters in a given string from left to right.

No adjacent characters should be identified in the final string.

Examples

  • "abbbaaccz" → "aaaccz" → "ccz" → "z"
  • "aabccdc" → "bccdc" → "bdc"
public class Solution {
public String deDup(String input) {
// Write your solution here
char[] charArr = input.toCharArray();
LinkedList<Character> stack = new LinkedList<>();
for (int i = 0; i < charArr.length; i++) {
char cur = charArr[i];
if (!stack.isEmpty() && stack.peekFirst() == cur) {
while (i + 1 < charArr.length && charArr[i] == charArr[i + 1]) {
i += 1;
}
stack.pollFirst();
} else {
stack.offerFirst(cur);
}
}
char[] res = new char[stack.size()];
for (int i = res.length - 1; i >= 0; i--) {
res[i] = stack.pollFirst();
}
return new String(res);
}
}

[LC] 82. Remove Adjacent Repeated Characters IV的更多相关文章

  1. [LC] 82. Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  2. leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)

    203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...

  3. 82. Remove Duplicates from Sorted List II && i

    题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such tha ...

  4. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  5. LeetCode 1100. Find K-Length Substrings With No Repeated Characters

    原题链接在这里:https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/ 题目: Give ...

  6. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  7. LC 722. Remove Comments

    Given a C++ program, remove comments from it. The program source is an array where source[i] is the  ...

  8. [LeetCode#82]Remove Duplicates from Sorted Array II

    Problem: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? F ...

  9. LeetCode OJ 82. Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

随机推荐

  1. Q3狂揽3亿美元净利润的特斯拉会让国内电动汽车厂商喜极而泣吗?

    作为电动汽车行业的标杆,特斯拉无疑是国内电动汽车厂商发展进程中重要的参考对象.而前段时间特斯拉身上出现的产能受阻.私有化风波.马斯克卸任董事长一职等事件,着实让国产电动汽车厂商惊出一身冷汗.毕竟如果特 ...

  2. Spring AOP 基本的使用

    1. jar包 2.全局配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...

  3. CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)

    题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的 ...

  4. eclipse上部署到tomcat不能自动部署maven管理的额jar包

  5. zabbix中文乱码解决

    问题现象: zabbix字体修改成中文后监控显示乱码 原因: 该问题是由于zabbix默认使用的是“DejaVuSans.ttf”(zabbix3.2.7默认使用的是“graphfont.ttf”), ...

  6. 吴裕雄--天生自然MySQL学习笔记:MySQL 函数

    ASCII(s) 返回字符串 s 的第一个字符的 ASCII 码. SELECT ASCII(CustomerName) AS NumCodeOfFirstChar FROM Customers; C ...

  7. MySQL--基础SQL--DCL

    DCL语句主要是DBA用来管理系统中的对象权限时使用,一般的开发人员很少使用. 1.创建一个数据库用户在z1,具有对sakila数据库中所有表的SELECT/INSERT权限: GRANT SELEC ...

  8. MYSQL8用户创建及权限操作

    MYSQL8创建.删除用户和授权.消权操作 上网找过资料说要进入mysql数据库在进行这些操作,我试了发现不进数据库和进入mysql数据库效果都一样 网上有的直接创建并赋权,像酱紫的: grant a ...

  9. 视图家族之mixins视图工具类与generics工具视图类

    视图家族之mixins视图工具类与generics工具视图类 一.mixins视图工具类 作用: 提供了几种后端视图(对数据资源进行曾删改查)处理流程的实现,如果需要编写的视图属于这五种,则视图可以通 ...

  10. Java面向对象(概述,构造函数,类与对象的关系,this关键字,成员、局部),匿名对象的调用,构造代码块(5)

    Java面向对象(概述,构造函数,类与对象的关系,this关键字,成员.局部),匿名对象的帝爱用,构造代码块(5)