fork download
  1. class RemoveConsecutiveChar {
  2.  
  3. public static void main(String[] args) {
  4. // TODO Auto-generated method stub
  5. String str = "dfffaaaaaghkklllllmaaannnnn";
  6.  
  7. int currLength = 0;
  8. int prevPos = 0;
  9. int currPos = 0;
  10. while ( currPos < str.length()) {
  11. if ( str.charAt(prevPos) == str.charAt(currPos)) {
  12. currLength++;
  13. currPos++;
  14. }
  15. else if ( currLength > 1) {
  16. str = str.substring(0 , prevPos) + str.substring(prevPos + currLength );
  17. prevPos = prevPos - 1 > 0 ? prevPos - 1 : 0;
  18. currPos = prevPos;
  19. currLength = 0;
  20. }
  21. else {
  22. prevPos = currPos;
  23. currLength = 0;
  24. }
  25. }
  26.  
  27. if ( currLength > 1) {
  28. str = str.substring(0 , prevPos) + str.substring(prevPos + currLength );
  29. }
  30.  
  31. System.out.println(str);
  32. }
  33.  
  34. }
  35.  
Success #stdin #stdout 0.04s 320576KB
stdin
Standard input is empty
stdout
dghm