Skip to content

Instantly share code, notes, and snippets.

@M-ZubairAhmed
Created January 25, 2017 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M-ZubairAhmed/cf4bd5d88d3ac75c4bec62f25a8ff229 to your computer and use it in GitHub Desktop.
Save M-ZubairAhmed/cf4bd5d88d3ac75c4bec62f25a8ff229 to your computer and use it in GitHub Desktop.
A simple algorithm to remove repetitions form the string. eg. azzbyyyx -> abyx
public class Main {
public static void main(String[] args) {
String originalString = "azzbyyyx";
StringBuilder str = new StringBuilder(originalString);
int loopCounter = 0;
int repetitionCounter = 0;
while (true) {
if (loopCounter < str.length() - 1) {
if (str.charAt(loopCounter) == str.charAt(loopCounter + 1)) {
str.deleteCharAt(loopCounter);
str.deleteCharAt(loopCounter);
repetitionCounter++;
} else {
loopCounter++;
}
} else {
if (repetitionCounter == 0) {
break;
} else {
loopCounter = 0;
repetitionCounter = 0;
}
}
}
System.out.println(str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment