Created
July 3, 2017 12:03
-
-
Save diemol/a0397b67b3283313329d46993567e9d7 to your computer and use it in GitHub Desktop.
Code to improve
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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