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. Arduino -- functions

    For controlling the Arduino board and performing computations. Digital I/O digitalRead() digitalWrit ...

  2. idea将web项目打成war包放在tomcat/webapps上运行

    1.进入Project Structure 或者 file -> Project Structure 或者 快捷键ctrl+alt+shift+s 2.选中Artifacts 3.点加号,然后如 ...

  3. 使用DOM4J生成XML文档

    package xml; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; imp ...

  4. mybatis的批量update

    方法有三种:1.通过java代码batch方式,xml文件只需一条update语句.java代码繁琐 2.xml使用foreach,“;”分割多条update语句,要求:jdbc的url需加上allo ...

  5. POJ 1019:Number Sequence 二分查找

    Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36013   Accepted: 10409 ...

  6. h5-任意元素居中

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. .NETCore部署步骤

    1.下载.NET CORE运行时 下载地址:https://dotnet.microsoft.com/download 2.windows安装下载的运行时 3.检查.是否安装成功 ,dotnet -- ...

  8. JAVA初学者——Hello,World!

    大家好,我是浩宇大熊猫 我本科专业学的是GIS(Geographical Information System),大学期间也学习了很多的编程语言,有C/C++/JAVA等 之前给我们授课的是韩冰老师, ...

  9. Class.forName(String className)解析

    功能: Class.forName(xxx.xx.xx)返回的是一个类 Class.forName(xxx.xx.xx)的作用是要求JVM查找并加载指定的类, 也就是说JVM会执行该类的静态代码段 一 ...

  10. getComputedStyle() 和 getPropertyValue()

    // getComputedStyle() 方法用于获取指定元素的 CSS 样式. // 获取的样式是元素在浏览器中最终渲染效果的样式. // getPropertyValue() 方法返回指定的 C ...