- Home
- /
- Tutorials
- /
- DSA Tutorial
- /
- Common String Problems
Strings
Common String Problems
Four patterns cover most string questions. Once you can see which one you are looking at, the code is short.
Reversing
Two ways to reverse
javascript
// Readable. O(n) time, O(n) space.
function reverse(s) {
return [...s].reverse().join("")
}
// In place on an array of characters. O(1) extra space.
function reverseChars(chars) {
let left = 0
let right = chars.length - 1
while (left < right) {
const temp = chars[left]
chars[left] = chars[right]
chars[right] = temp
left++
right--
}
return chars
}