Skip to content

Instantly share code, notes, and snippets.

@Maslor
Created August 6, 2015 22:10
Show Gist options
  • Save Maslor/b4f7e5bc745cd19636bf to your computer and use it in GitHub Desktop.
Save Maslor/b4f7e5bc745cd19636bf to your computer and use it in GitHub Desktop.
class that reverses a long string efficiently with StringBuilder
import java.lang.StringBuilder;
public class ReverseLonger {
public static String shorterReverseLonger(String a, String b) {
if (a.length() < b.length()) {
return a + reverse(b) + a;
} else {
return b + reverse(a) + b;
}
}
public static String reverse(String str) {
String reverse = new StringBuilder(str).reverse().toString();
return reverse;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment