Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public String dash(String S, int K) {
- if (S.isEmpty()) {
- return S;
- }
- int oldDash = 0;
- char[] array = S.toCharArray();
- for (int i = 0; i < array.length; i++) {
- if (array[i] == '-') {
- oldDash++;
- }
- }
- int realLen = array.length - oldDash;
- int newDash = 0;
- if (realLen % K == 0) {
- newDash = realLen / K - 1;
- } else {
- newDash = realLen / K;
- }
- char[] newArray = new char[realLen + newDash];
- int iOld = array.length - 1;
- int count = 0;
- int iNew = newArray.length - 1;
- while (iNew >= 0) {
- if (count == K) {
- newArray[iNew--] = '-';
- count = 0;
- } else if (array[iOld] != '-') {
- newArray[iNew--] = Character.toUpperCase(array[iOld--]);
- count++;
- } else {
- iOld--;
- }
- }
- return new String(newArray);
- }
- public static void main(String[] args) {
- Dash d = new Dash();
- System.out.println(d.dash("2-4A0r7-4K", 3));
- }
Advertisement
Add Comment
Please, Sign In to add comment