Java tutorial
//package com.java2s; public class Main { private static String deriveLongerString(String str) { StringBuilder sb = new StringBuilder(str); StringBuilder builder = new StringBuilder(); builder.append(sb.toString().toLowerCase()); builder.append(sb.toString().toUpperCase()); StringBuilder result = new StringBuilder(); result.append(sb); result.append(mix(builder.toString())); result.append(mix(builder.reverse().toString())); return result.toString(); } private static String mix(String str) { String result = str; String holder; int iters = str.length() / 4; for (int i = 0; i < iters; i++) { holder = mixStep(result); result = holder; } return result; } private static String mixStep(String str) { if (str == null || str.isEmpty()) { return ""; } if (str.length() == 1) { return str; } if (str.length() == 2) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } StringBuilder sb = new StringBuilder(); String char1 = String.valueOf(str.charAt(0)); String char2 = String.valueOf(str.charAt(1)); String char3 = String.valueOf(str.charAt(2)); if ((char1.compareTo(char2) > 0) && (char1.compareTo(char3) < 0)) { return sb.append(mixStep(str.substring(2))).append(str.charAt(1)).append(str.charAt(0)).toString(); } else if ((char1.compareTo(char2) > 0) && (char1.compareTo(char3) > 0)) { String mixReverse = (new StringBuilder(mixStep(str.substring(2)))).reverse().toString(); return sb.append(str.charAt(1)).append(mixReverse).append(str.charAt(0)).toString(); } else if ((char1.compareTo(char2) < 0) && (char1.compareTo(char3) > 0)) { return sb.append(str.charAt(0)).append(mixStep(str.substring(2))).append(str.charAt(1)).toString(); } else if ((char1.compareTo(char2) < 0) && (char1.compareTo(char3) < 0)) { String mixReverse = (new StringBuilder(mixStep(str.substring(2)))).reverse().toString(); return sb.append(str.charAt(0)).append(mixReverse).append(str.charAt(1)).toString(); } return sb.append(str.charAt(1)).append(str.charAt(0)).append(mixStep(str.substring(2))).toString(); } }