Reverting a mutable string implies relocating at least half of the characters (if you do it in-place), so that is O(n) complexity. The code you posted has two loops, confirming it's O(n).

On a more thoeretical note, the entire StringBuilder implementation could be devoted to O(1) inversion, such that you only set a flag and then all the methods honor it by interpreting the array contents either front-to-back or back-to-front. I say "theoretical" because there's no point in increasing the complexity of everything, making it slower as well, just to have O(1) string inversion.

Answer from Marko Topolnik on Stack Overflow
🌐
Medium
medium.com › @AlexanderObregon › javas-stringbuilder-reverse-method-explained-b2b701ee2029
Java’s StringBuilder.reverse() Method Explained | Medium
October 3, 2024 - This technique is highly efficient because it only requires traversing the string once and swapping characters, resulting in a time complexity of O(n), where n is the length of the string.
Discussions

c# - Big O notation for Reverse String - Code Review Stack Exchange
I have here some methods which reverse string characters. Example if given string is "HELLO", it will returns "OLLEH". Are my big o notation for these approaches correct? And which approach is the ... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
March 10, 2020
What is the most efficient algorithm for reversing a String in Java? - Stack Overflow
Create a new reversed String by ... one in reverse order from the original String to a blank String/StringBuilder/char[]. Exchange all characters in the first half of the String with its corresponding position in the last half (i.e. the ith character gets swapped with the (length-i-1)th character). The thing is that all of them have the same runtime complexity: ... More on stackoverflow.com
🌐 stackoverflow.com
March 14, 2010
Java String manipulation: Time and Space complexity using StringBuilder - Stack Overflow
I was recently interviewed at a XXX company and was asked to write a code to reverse the words in the input. Input: "My name is Joe" Output: "yM eman si eoJ" I wrote below code which compiled an... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
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.
🌐
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
The time complexity of this algorithm is O(n/2)I mean O(n) where n is the length of String. Ideally, whenever you need to reverse a String in your application, you should be using the reverse() method of StringBuilder.
🌐
Medium
medium.com › @techie_arbaaz › 10-common-coding-interview-questions-on-string-using-java-4a19fc4d9ce8
Common Coding Interview Questions on String using Java | by Techie Arbaaz | Medium
August 14, 2025 - public class ReverseString { public static void main(String[] args) { String input = "Hello World"; StringBuilder sb=new StringBuilder(); for(int i=str.length()-1;i>=0;i--) { sb.append(str.charAt(i)); } System.out.println("Reversed String: " + reversed); } Time Complexity: O(n) The reverse() method iterates through the StringBuilder to reverse the string.
Top answer
1 of 2
4

There are a couple of constructs in there that can be improved.

  • string.Join("", charArray), repeated a lot. string has a constructor that takes a char[], which is a lot faster. They are both linear time, which your question seems to focus on, but in terms of actual efficiency the difference is nearly two orders of magnitude in some tests (actual impact of course varies).
  • new Stack(), so.. the old stack, from the .NET 1.1 days? Don't use that one, use Stack<T>. Especially if the things you're putting in them are value types, which would have to be boxed in the old non-generic Stack.

For ReverseString_ForEachConcat, I don't agree that the time complexity is O(n). At every step, the old string is copied over into the new string, with something concatenated in front of it. So in the second iteration there is 1 copied character, in the third iteration there are 2 copied characters etc. That's a classic O(n²) pattern.

I think we could also argue about whether or not it takes constant space. The many old versions of the string can disappear quickly, but while the concatenation is happening, both the old string and the result of the concatenation need to exist. In the last iteration, that means that while the final result is being made (the size of which doesn't count), there is an other string in play of nearly the same size, so O(n) worth of auxiliary space.

2 of 2
2

I was going to write this as a comment, but I have too much to talk about. When I first read the question, what was screaming at me is "WHY?!" Why go through such gymnastics? Are you intentionally trying to reinvent-the-wheel? If you are, then please tag the question with that tag.

If you are wanting to learn C#, and equally important, .NET, then I would suggest your focus should be on relying upon the framework.

For a simple Americanized string, where each character in the string is its own entity, you could try something like:

public static string ReverseString(string str)
{
    char[] arr2 = str.ToCharArray();
    Array.Reverse(arr2);
    return new string(arr2);
}

There are also examples here on CR where the reversing is done by only going halfway through the char array. The endpoints are swapped, and then indices are moved inward.

Note the above only works for some strings. If your input string contains certain characters from different cultures, these characters are known as surrogate pairs. You may loosely think of the pair as a composite. For such things, you do not want to reverse the individual characters because it breaks the surrogate relationship. Instead, you would use .NET and look into the StringInfo class (part of System.Globalization). The link provided shows how to honor surrogates.

🌐
AlgoCademy
algocademy.com › link
Reverse String in Java | AlgoCademy
A naive solution would involve swapping characters from the beginning and end of the string until we reach the middle. However, this approach would typically run in O(n) time complexity, which does ...
Find elsewhere
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › reverse string in java
Reverse String in Java | Using Various Methods with Examples
January 8, 2024 - Q3. What is the time complexity of reversing a string using different methods? Answer: Reversing a string using StringBuilder or manually with a character array has a time complexity of O(n), where n is the length of the string.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Java67
java67.com › 2016 › 06 › how-to-reverse-string-in-place-in-java.html
[Solved] How to reverse a String in place in Java? Example | Java67
One of the common Java coding interview questions is to write a program to reverse a String in place in Java, without using additional memory. You cannot use any library classes or methods like StringBuilder to solve this problem. This restriction is placed because StringBuilder and StringBuffer class define a reverse() method which can easily reverse the given String.
🌐
Stack Overflow
stackoverflow.com › a › 19814248
Newest Questions - Stack Overflow
Stack Overflow | The World’s Largest Online Community for Developers
🌐
Stack Overflow
stackoverflow.com › questions › 55252335 › java-string-manipulation-time-and-space-complexity-using-stringbuilder
Java String manipulation: Time and Space complexity using StringBuilder - Stack Overflow
public String reverseWords(String s) { StringBuilder sb = new StringBuilder(); s += ' '; for (int i = 1; i < s.length(); i++) { if (s.charAt(i) == ' ' && s.charAt(i-1) != ' ') { if (sb.length()>0) sb.append(' '); for (int j = i-1; j >= 0 && s.charAt(j) != ' '; j--) sb.append(s.charAt(j)); } } return sb.toString(); } Interviewer then asked with Time and Space complexity of the code to which I said: Space complexity looks O(N) (where N is number of characters in input) and Time complexity is Quadratic.
🌐
Coderanch
coderanch.com › t › 545713 › java › string-reverse
string reverse O(N) or O(N/2) (Java in General forum at Coderanch)
July 17, 2011 - I think we cant reduce the string reverse to O(N/2)? Please correct me if i am wrong. Thanks Chandrashekar ... Note that there is no O(N/2) - it is linear complexity, and asymptotically the same as O(N). In this particular case, performance is probably improved much more by using a StringBuilder instead of string concatenation.
🌐
CodeGym
codegym.cc › java blog › random › different ways to reverse a string in java
Different Ways to Reverse a String in Java
February 1, 2023 - The time complexity of the above code is O(N), and as we have iterated over all the n elements the space complexity of the above code is O(N). Now let us read about the next process which is reversing the string using the string builder, so now let us read about it in detail: We can also reverse the string in Java by using the StringBuilder class.
🌐
Techie Delight
techiedelight.com › home › string › iterative solution to reverse a string in c++ and java
Iterative solution to reverse a string in C++ and Java | Techie Delight
September 19, 2025 - A simple approach is to use strrev() function in C, or std::reverse in C++, or StringBuilder.reverse() method in Java. ... We can easily reverse a given string using the stack data structure. The idea is to push each character of the string into a stack and then start filling the input string (starting from index 0) by popping characters from the stack until it is empty. Following is the C++ and Java implementation of the idea: ... The time complexity of the above solution is O(n) and the auxiliary space used by the program is O(n) for stack.
🌐
Interviewing.io
interviewing.io › questions › reverse-string
How to Reverse a String [Interview Question + Solution]
September 13, 2018 - For example, there is StringBuilder in Java. Convert the string to a character array char_array. Initialize two pointers, start and end, to point at the start and the end of the string, respectively. Loop until start is less than end. Swap the characters at start and end. Increment start and decrement end. Convert the character array back to a string and return it. ... function reverseString(string) { let charArray = string.split(""); let start = 0; let end = charArray.length - 1; while (start < end) { swap(charArray, start, end); start += 1; end -= 1; } return charArray.join(""); } function swap(charArray, start, end) { // using destructuring assignment to swap the characters [charArray[start], charArray[end]] = [charArray[end], charArray[start]]; }
🌐
GitHub
github.com › rootusercop › TimeComplexityOfPredefinedMethodsInJava › blob › master › String, StringBuilder and StringBuffer class methods
TimeComplexityOfPredefinedMethodsInJava/String, StringBuilder and StringBuffer class methods at master · rootusercop/TimeComplexityOfPredefinedMethodsInJava
PLEASE NOTE THAT reverse() method is NOT PRESENT in String class of JAVA. The reason being String class · in JAVA is immutable. However, for some reason substring() method is present in String class even though String · is immutable in JAVA. Also StringBuilder and StringBuffer classes each have substring() method.
Author   rootusercop
🌐
Debugcn
debugcn.com › en › article › 55948001.html
Time complexity of StringBuilder reverse method - DebugCN
public AbstractStringBuilder reverse() { boolean hasSurrogate = false; int n = count - 1; for (int j = (n-1) >> 1; j >= 0; --j) { char temp = value[j]; char temp2 = value[n - j]; if (!hasSurrogate) { hasSurrogate = (temp >= Character.MIN_SURROGATE && temp <= Character.MAX_SURROGATE) || (temp2 >= Character.MIN_SURROGATE && temp2 <= Character.MAX_SURROGATE); } value[j] = temp2; value[n - j] = temp; } if (hasSurrogate) { // Reverse back all valid surrogate pairs for (int i = 0; i < count - 1; i++) { char c2 = value[i]; if (Character.isLowSurrogate(c2)) { char c1 = value[i + 1]; if (Character.isHighSurrogate(c1)) { value[i++] = c1; value[i] = c2; } } } } return this; } Here's a link to the doc: documentation Which is the time complexity?