Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. package stringeliminator;
  2.  
  3. public class Eliminator {
  4.  
  5.     public String eliminate(String s) {
  6.  
  7.         StringBuilder sb = new StringBuilder(s);
  8.        
  9.         int actualLength = sb.length();
  10.  
  11.         for (int i=0; i < actualLength-1; i++){
  12.            
  13.             boolean equalChars = sb.charAt(i) == sb.charAt(i+1);
  14.        
  15.             if(equalChars){
  16.                 sb.deleteCharAt(i);
  17.                 sb.deleteCharAt(i);
  18.                 i = 0;
  19.                 actualLength = sb.length();
  20.             }
  21.            
  22.         }
  23.        
  24.         if (sb.charAt(0) == sb.charAt(1)){
  25.             sb.deleteCharAt(0);
  26.             sb.deleteCharAt(0);
  27.         }
  28.        
  29.         return sb.toString();
  30.     }
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement