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. 4 react 简书 引入 redux 的 combineReducers 对 redux 数据进行管理

    1. src 下的 common 下的 header 创建 store 文件夹 下创建 reducer.js # src/common/header/store/reducer.js const st ...

  2. 线性数据结构案例1 —— 单向链表中获取倒数k个节点

    一.介绍  先遍历整个链表获取链表长度length,然后通过 (length-index) 方式得到我们想要节点在链表中的位置. 二.代码 public Node findLastIndexNode( ...

  3. Hibernate(八)--session的两种获取方式

    openSession getCurrentSession Hibernate有两种方式获得session,分别是: openSession和getCurrentSession他们的区别在于1. 获取 ...

  4. 18 12 30 新建一个 django project

    1. 新建一个 django project 1 2 django-admin.py startproject project_name 特别是在 windows 上,如果报错,尝试用 django- ...

  5. 18 12 28 css 浮动 定位

    浮动 浮动特性 1.浮动元素有左浮动(float:left)和右浮动(float:right)两种 2.浮动的元素会向左或向右浮动,碰到父元素边界.其他元素才停下来 3.相邻浮动的块元素可以并在一行, ...

  6. 系统学习python第七天学习笔记

    1.get方法用法补充 info = {'name':'王刚蛋','hobby':'铁锤'} while True: data = input('请输入:') val = info.get(data, ...

  7. 一维消消乐(DP)

    一维消消乐是一款非常简单的游戏.有n颗珠子排成一排,每一颗珠子有一个价值w(可能是负数). 游戏是这样,你可以选择如若干对相邻的珠子,让他们同时消去.每一对珠子的消失,都会使得总分数加上两颗珠子相乘的 ...

  8. js.console携程近期低价机票信息

    !function(){var city = {"SHA":"上海虹桥","PVG":"上海浦东","YIW& ...

  9. jq切换选择项

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

  10. IO流处理文件读取到字节数组,再从字节数组读取到文件,Java实现

    package cn.zhang.io; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; impo ...