Skip to content

Instantly share code, notes, and snippets.

@orhanobut
Last active December 23, 2015 12:49
Show Gist options
  • Save orhanobut/6637761 to your computer and use it in GitHub Desktop.
Save orhanobut/6637761 to your computer and use it in GitHub Desktop.
Reverse a string in-space without an extra buffer. Complexity = O (n)
public static String reverse(String s) {
int lenght = s.length();
int last = s.length() - 1;
char[] temp = s.toCharArray();
for (int i = 0; i < lenght / 2; i++) {
char c = temp[i];
temp[i] = temp[last - i];
temp[last - i] = c;
}
return new String(temp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment