sweet1cris

Untitled

Sep 7th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1. public class Solution {
  2.   public String deDup(String input) {
  3.     // Write your solution here.
  4.     if (input == null || input.length() <= 1) {
  5.       return input;
  6.     }
  7.     char[] array = input.toCharArray();
  8.     int slow = 0;
  9.     for (int fast = 1; fast < array.length; fast++) {
  10.       if (slow == -1 || array[slow] != array[fast]) {
  11.         array[++slow] = array[fast];
  12.       } else {
  13.         while (fast + 1 < array.length && array[fast] == array[fast + 1]) {
  14.           fast++;
  15.         }
  16.         slow--;
  17.       }
  18.     }
  19.     return new String(array, 0 , slow + 1);
  20.   }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment