String Reversal Approaches in DSA Typescript - Time & Space Complexity
We want to understand how the time needed to reverse a string changes as the string gets longer.
How does the work grow when the input string size increases?
Analyze the time complexity of the following code snippet.
function reverseString(str: string): string {
const chars: string[] = [];
for (let i = str.length - 1; i >= 0; i--) {
chars.push(str[i]);
}
return chars.join('');
}
This code creates a reversed string by appending characters from the end of the input string to an array and then joining them.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each character of the string once.
- How many times: Exactly once for each character, from last to first.
As the string gets longer, the number of steps grows roughly the same as the string length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The work grows linearly with the input size.
Time Complexity: O(n)
This means the time to reverse the string grows directly in proportion to the string length.
[X] Wrong: "Reversing a string is instant or constant time because it's just one operation."
[OK] Correct: Each character must be handled one by one, so the time grows with the string size, not fixed.
Understanding how string reversal scales helps you reason about similar problems where you process each item once.
"What if we used recursion to reverse the string instead? How would the time complexity change?"
Practice
split('') method do when applied to a string in TypeScript?Solution
Step 1: Understand the split method
Thesplit('')method breaks a string into an array of characters by splitting at every empty string between characters.Step 2: Effect on string
For example, "hello".split('') becomes ["h", "e", "l", "l", "o"].Final Answer:
It converts the string into an array of individual characters. -> Option BQuick Check:
split('') = array of chars [OK]
- Thinking split('') reverses the string
- Confusing split('') with join()
- Assuming split('') removes spaces
str using built-in methods in TypeScript?Solution
Step 1: Split string into array
Usestr.split('')to convert string into array of characters.Step 2: Reverse and join
Usereverse()on array, thenjoin('')to form reversed string.Final Answer:
str.split('').reverse().join('') -> Option AQuick Check:
split + reverse + join = reversed string [OK]
- Calling reverse() directly on string
- Using join() before reverse()
- Using reverse() as a standalone function
const str = "abcde";
const reversed = str.split('').reverse().join('');
console.log(reversed);Solution
Step 1: Split string into array
"abcde".split('') becomes ["a", "b", "c", "d", "e"].Step 2: Reverse and join array
Reversing gives ["e", "d", "c", "b", "a"], joining results in "edcba".Final Answer:
"edcba" -> Option DQuick Check:
split + reverse + join = "edcba" [OK]
- Expecting original string output
- Mixing up reversed characters
- Forgetting to join after reverse
function reverseString(s: string): string {
return s.reverse();
}Solution
Step 1: Check string methods
Strings in TypeScript do not have areverse()method directly; only arrays do.Step 2: Correct approach
Convert string to array withsplit(''), reverse array, then join back.Final Answer:
Strings do not have a reverse() method directly. -> Option AQuick Check:
reverse() works on arrays, not strings [OK]
- Trying to call reverse() on string directly
- Ignoring need to split string first
- Assuming reverse() returns void
reverseString('12345')? function reverseString(s: string): string {
let reversed = '';
for (let i = s.length - 1; i >= 0; i--) {
reversed += s[i];
}
return reversed;
}Solution
Step 1: Loop from end to start
The loop starts at index 4 (last char '5') and moves backward to 0.Step 2: Build reversed string
Characters are appended in reverse order: '5', '4', '3', '2', '1'.Final Answer:
"54321" -> Option CQuick Check:
Loop backward concatenates reversed string [OK]
- Looping forward instead of backward
- Off-by-one errors in loop index
- Not initializing reversed string properly
Ready to prove it?
Pass the test and this pattern moves unknown
