Skip to content

Instantly share code, notes, and snippets.

@diemol
Created July 3, 2017 12:03
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 diemol/a0397b67b3283313329d46993567e9d7 to your computer and use it in GitHub Desktop.
Save diemol/a0397b67b3283313329d46993567e9d7 to your computer and use it in GitHub Desktop.
Code to improve
class Sample {
public String solution(String S) {
int len = S.length();
if (len >= 2 && len <= 100) {
char[] charList = new char[len];
charList = S.toCharArray();
int charDash = 0;
String sol = "";
int count = 0;
for (int i = 0; i < len; i++) {
if (charList[i] != ' ' &&
charList[i] != '-') {
if (Character.getNumericValue(charList[i]) <= 9 &&
Character.getNumericValue(charList[i]) >= 0) {
if (count != 0 && count % 3
== 0) {
sol = sol + "-" +
charList[i];
} else {
sol += charList[i];
}
if (charDash == 2) {
count = count + 2;
charDash=0;
} else {
count++;
}
} else {
return "INVALID INPUT: expected Numeric values";
}
} else if (i!=0 && charList[i] ==
'-' && charList[i - 1] == '-') {
charDash = 2;
}
}
return sol;
} else {
return "INVALID INPUT: expected atleast characters";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment