You say you want to know the most efficient way and you don't want to know some standard built-in way of doing this. Then I say to you: RTSL (read the source, luke):

Check out the source code for AbstractStringBuilder#reverse, which gets called by StringBuilder#reverse. I bet it does some stuff that you would not have considered for a robust reverse operation.

Answer from Tom on Stack Overflow
🌐
LeetCode
leetcode.com › problems › reverse-string
Reverse String - LeetCode
The input string is given as an array of characters s. You must do this by modifying the input array in-place [https://en.wikipedia.org/wiki/In-place_algorithm] with O(1) extra memory.
Discussions

What is the time complexity of reversing a string?
It should be simply O(n) where n is the length of string. If you think of a string as an array, you simply swap characters between left and right ends until they cross over the mid-point — which means O(n/2) and simplified to O(n). More on reddit.com
🌐 r/algorithms
13
10
October 23, 2020
How to begin Reverse a String algorithm?
So I’m trying to talk out loud this algorithm and break the problem down piece by piece. Here’s what I understand so far and hopefully the forum can help fill in the blanks. So I understand I probably will need to store this string into an array. Once the string is into the array I would ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
March 6, 2018
Can anyone explain how to reverse a integer number?
That's hardly cheating, and is now I'd do it unless that method was explicitly forbidden (I had that once). Your div/mod way is more complicated, and in my experience, likely doesn't have much in the way of noticeable performance advantages anyway. Abusing strings is often a good solution surprisingly (but consider each case individually). More on reddit.com
🌐 r/learnpython
26
38
February 2, 2022
Fastest way to reverse a string - and it's not extended string splicing?
I was told there's a faster method with some optimisations possible. If reversing strings is a bottleneck needing optimization, then your codebase might have bigger issues. More on reddit.com
🌐 r/learnpython
70
245
August 12, 2020
🌐
Interviewing.io
interviewing.io › questions › reverse-string
How to Reverse a String [Interview Question + Solution]
September 13, 2018 - Space Complexity: O(n), as we use a stack that stores n characters. When a string is reversed, the last character becomes the first character, and the first character becomes the last character.
🌐
Medium
medium.com › @rubenosmaralvarado › reverse-a-string-algorithm-195529cd13f9
Reverse a String Algorithm
July 19, 2025 - To go from input to output, we need to write a function that reverses the string. We need to define a function that performs this task, and for that, we need to develop an algorithm.
🌐
Cloud Native Journey
cloudnativejourney.wordpress.com › 2023 › 05 › 29 › exploring-string-reversal-algorithms-techniques-for-reversing-text-efficiently
Exploring String Reversal Algorithms: Techniques for Reversing Text Efficiently – Cloud Native Journey
May 29, 2023 - Recursion can also be employed to reverse a string. The recursive algorithm breaks down the problem into smaller subproblems. It recursively calls the reverse function on the substring excluding the first character and appends the first character at the end. The base case is when the string length becomes 0 or 1, in which case the string itself is returned.
🌐
GeeksforGeeks
geeksforgeeks.org › program-reverse-string-iterative-recursive
Reverse a String - GeeksforGeeks
September 4, 2023 - Below is the implementation of the above approach: ... The recursive algorithm to reverse a string works by swapping the first and last characters until the middle of the string is reached.
🌐
DEV Community
dev.to › alisabaj › reversing-a-string-in-place-4mdh
Reversing a String in Place - DEV Community
June 4, 2020 - We'll set left equal to 0, so that it starts at the beginning, and we'll set right equal to the length of the string minus 1, so that it starts at the end of the string (remember that indexing starts at 0). function reverseString(str) { let ...
Find elsewhere
🌐
Medium
medium.com › swlh › how-to-reverse-a-string-16a2acc1dab8
How to Reverse a String. The only way to get better at solving… | by Jake Zhang | The Startup | Medium
August 25, 2020 - How to Reverse a String The only way to get better at solving algorithms and data structures is to power through a few Can you write a function that reverses an inputted string without using the …
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › easiest-way-to-reverse-a-string
Easiest Way to Reverse a String - GeeksforGeeks
February 5, 2024 - The easiest way to reverse a string is to use the inbuilt function of respective languages. There is a direct function in the “algorithm” header file for doing reverse that saves our time when programming.
🌐
GeeksforGeeks
geeksforgeeks.org › string-reverse
String Reverse – Complete Tutorial - GeeksforGeeks
The idea is to start at the last character of the string and move backward, appending each character to a new string res. This new string res will contain the characters of the original string in reverse order.
Published   October 1, 2024
🌐
Reddit
reddit.com › r/algorithms › what is the time complexity of reversing a string?
r/algorithms on Reddit: What is the time complexity of reversing a string?
October 23, 2020 -

I'm getting ready for an interview and practicing time complexity and I'm ok at recognizing the complexity for the code I've written myself but have a harder time when I throw in methods from the java library.

For example, I read that the method concat was similar to += and that concat time complexity was O(n^2). So if I reverse a string with the code below does that mean my time complexity is O(n^2)? With a space complexity of O(n) since? Where n is the length of the string.

String str="hello"
String s="";
for(int i =str.length()-1; i>=0;i--){
s+=str.substring(i,i+1);
}

With this code is the space complexity O(n) and the time complexity O(n)? Where n is the length of the string.

String str="hello";
char[] cArr = str.toCharArray();
int i =0;
int j = cArr.length-1;
char temp;
while(i<j){
    temp =cArr[i];
    cArr[i]=cArr[j];
    cArr[j]=temp;
    i++;
    j--;
}
str=String.valueOf(cArr);
Top answer
1 of 5
17
It should be simply O(n) where n is the length of string. If you think of a string as an array, you simply swap characters between left and right ends until they cross over the mid-point — which means O(n/2) and simplified to O(n).
2 of 5
8
I don't know the time complexity of internal java lib functions by heart- I'll leave it to someone else to comment on that. What I do want to offer is a technique to sanity check your understanding. Asymptotic time complexity is very much on the numerical / theoretical side of things, but the actual computation time will reflect this behavior once the input size grows large enough. What you can do is repeatedly time the function on various input sizes spanning many orders of magnitude until you reach some limit (probably where the computation starts taking multiple seconds). You can then graph input size vs computation time, and see if its constant, linear, quadratic, logarithmic, etc., by eye. If you want a more precise measurement, take the log of both the input sizes and the computation times (so its a log-log plot), and a best-fit line through your data points (using a linear regression), the slope is the exponent of your variable in your measured time complexity (so a slope of 2 corresponds to observed quadratic time behavior). This isn't perfect, there can easily be huge constants throwing off your measured time complexity, you'll likely drop any logarithmic factors, etc. Its more of a coarse sanity check- if you think the algorithm is O(N2 ) but you experiment and see a near-straight line, and the log-log plot has a slope of 1, then you can reason that its likely not quadratic, its behaving more as a linear-time algorithm, and you should recheck your calculations.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
How to begin Reverse a String algorithm? - JavaScript
March 6, 2018 - So I’m trying to talk out loud this algorithm and break the problem down piece by piece. Here’s what I understand so far and hopefully the forum can help fill in the blanks. So I understand I probably will need to store this string into an array. Once the string is into the array I would ...
🌐
AlgoDaily
algodaily.com › challenges › reverse-a-string
AlgoDaily - Daily coding interview questions. Full programming interview prep course and software career coaching.
Programming interview prep bootcamp with coding challenges and practice. Daily coding interview questions. Software interview prep made easy.
🌐
coding algorithms
tekmarathon.com › 2012 › 10 › 05 › algorithm-to-reverse-a-string
algorithm to reverse a string array in O(n/2) complexity | coding algorithms
November 18, 2015 - String str = “welcome to javaj2ee forum”; //take two pointers, one pointing to the start of String and another to the end of the string // while traversing the String increment starting pointer and decrement ending pointer and continue this till we reach the middle of the string by this time String will be reversed int i = 0; int j = str.length(); String tmp;
🌐
Blogger
javarevisited.blogspot.com › 2016 › 03 › how-to-reverse-string-in-place-in-java.html
How to Reverse a String in place in Java - Example
Ideally, whenever you need to reverse a String in your application, you should be using the reverse() method of StringBuilder. This method reverses the character sequence of the String in place.
🌐
AlgoCademy
algocademy.com › link
Reverse String in Java | AlgoCademy
An optimized solution involves using a character array to store the reversed string. We can iterate over the original string from the end to the beginning and fill the character array. This approach ensures O(n) time complexity and O(n) space complexity. Here is a step-by-step breakdown of the optimized algorithm:
🌐
Rosetta Code
rosettacode.org › wiki › Reverse_a_string
Reverse a string - Rosetta Code
March 28, 2026 - Monadic reverse (| ) verb reverses a string or list of any shape ... (include "string" "algorithm") (main (decl std::string s) (std::getline std::cin s) (std::reverse (s.begin) (s.end)) (prn s))
🌐
Algorithmexamples
cplusplus.algorithmexamples.com › web › strings › reverse_string.html
2000+ Algorithm Examples in Python, Java, Javascript, C, C++, Go, Matlab, Kotlin, Ruby, R and Scala
// // Reverse String in C++ // // The All ▲lgorithms Project // // https://allalgorithms.com/strings // https://github.com/allalgorithms/cpp // // Contributed by: Tushar Kanakagiri // Github: @tusharkanakagiri // #include <stdio.h> /* function prototype for utility function to reverse a string from begin to end */ void reverse(char *begin, char *end); /*Function to reverse words*/ void reverseWords(char *s) { char *word_begin = s; char *temp = s; /* temp is for word boundry */ /*STEP 1 of the above algorithm */ while (*temp) { temp++; if (*temp == '\0') { reverse(word_begin, temp - 1); } els
🌐
Codestandard
codestandard.net › articles › reverse-string
How to reverse a string - CodeStandard.net
The same is relevant for the pointer ... question. to reverse a string it means the algorithm should swap the left part of the string with right part of the string....