Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- public String deDup(String input) {
- // Write your solution here.
- if (input == null || input.length() <= 1) {
- return input;
- }
- char[] array = input.toCharArray();
- int slow = 0;
- for (int fast = 1; fast < array.length; fast++) {
- if (slow == -1 || array[slow] != array[fast]) {
- array[++slow] = array[fast];
- } else {
- while (fast + 1 < array.length && array[fast] == array[fast + 1]) {
- fast++;
- }
- slow--;
- }
- }
- return new String(array, 0 , slow + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment