Created
June 24, 2023 14:12
-
-
Save deepaksinghvi/0047d818849853260738c635d0f6b571 to your computer and use it in GitHub Desktop.
Unmix My Strings
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
public class UnmixStrings { | |
public static void main(String[] args) { | |
System.out.println(String.format("Input: hTsii s aimex dpus rtni.g, Output: %s",unmix("hTsii s aimex dpus rtni.g"))); | |
System.out.println(String.format("Input: 214365, Output: %s",unmix("214365"))); | |
System.out.println(String.format("Input: badce, Output: %s",unmix("badce"))); | |
} | |
public static String unmix(String str) { | |
StringBuilder sb = new StringBuilder(); | |
int length = str.length(); | |
boolean oddLength = !(length%2==0); | |
int i=0; | |
for(;i<length-1;i=i+2) | |
{ | |
sb.append("" + str.charAt(i + 1) + str.charAt(i)); | |
} | |
if (oddLength) { | |
sb.append(str.charAt(i)); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment