Skip to content

Instantly share code, notes, and snippets.

@deepaksinghvi
Created June 24, 2023 14:12
Show Gist options
  • Save deepaksinghvi/0047d818849853260738c635d0f6b571 to your computer and use it in GitHub Desktop.
Save deepaksinghvi/0047d818849853260738c635d0f6b571 to your computer and use it in GitHub Desktop.
Unmix My Strings
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