Flip Characters
The Problem
Given an array of characters, reverse the array in-place — that is, modify the original array so its characters appear in reverse order.
Input: chars = ['h', 'e', 'l', 'l', 'o']
Output: chars is modified to ['o', 'l', 'l', 'e', 'h']
This is the character-array equivalent of reversing an integer array — the same two-pointer mechanics, applied to chars.
Examples
Example 1
Input: ['h', 'e', 'l', 'l', 'o']
Output: ['o', 'l', 'l', 'e', 'h']
Example 2
Input: ['A', 'B', 'C', 'D']
Output: ['D', 'C', 'B', 'A']
Example 3 — single character
Input: ['X']
Output: ['X'] (unchanged)
Intuition
To reverse a sequence, the first element must become the last, the second must become the second-to-last, and so on. Every character has a mirror partner equidistant from the opposite end. We just need to swap each pair.
Two pointers are perfect for this: left starts at index 0 (the first character), right starts at index n-1 (the last character). Swap the pair, then move both inward. Repeat until they meet.
---
config:
theme: base
themeVariables:
primaryColor: "#dbeafe"
primaryBorderColor: "#3b82f6"
primaryTextColor: "#1e3a5f"
lineColor: "#64748b"
secondaryColor: "#ede9fe"
tertiaryColor: "#fef9c3"
---
flowchart TB
subgraph S0["Start — left=0, right=4"]
direction LR
P0L(["left"]) --> C0["h"] --- C1["e"] --- C2["l"] --- C3["l"] --- C4["o"]
C4 --> P0R(["right"])
end
subgraph S1["Swap 'h' ↔ 'o' — left=1, right=3"]
direction LR
P1L(["left"]) --> D0["o"] --- D1["e"] --- D2["l"] --- D3["l"] --- D4["h"]
D3 --> P1R(["right"])
end
subgraph S2["Swap 'e' ↔ 'l' — left=2, right=2"]
direction LR
P2L(["left"]) --> E0["o"] --- E1["l"] --- E2["l"] --- E3["e"] --- E4["h"]
E2 --> P2R(["right"])
end
subgraph S3["left = right = 2 → loop ends"]
DONE(["✓ ['o','l','l','e','h']"])
end
S0 -->|"swap + left++ + right--"| S1
S1 -->|"swap + left++ + right--"| S2
S2 -->|"left ≥ right"| S3
Flipping ['h','e','l','l','o'] in-place — two swaps bring the array to its reversed state; the middle character needs no swap.
Applying the Diagnostic Questions
| Check | Answer for Flip Characters |
|---|---|
| ✅ Two positions simultaneously? | Yes — chars[left] and chars[right] are read and swapped together at every step |
| ✅ One near start, one near end? | Yes — left = 0, right = n-1 |
| ✅ Both move inward? | Yes — left++, right-- after every swap |
| ✅ Simple work at each step? | Yes — one swap per iteration |
Every box is checked with nothing extra needed. This is the purest direct application — the template and the algorithm are identical.
Why does every element have exactly one partner? Because reversal is a bijection: element at position i maps to position n-1-i. Two pointers exploit this directly — left tracks “the element at distance 0 from the left” and right tracks “the element at distance 0 from the right.” Every step, both advance one position inward, so the i-th iteration handles the i-th mirror pair. When left >= right, all pairs have been processed.
What breaks if you use one pointer instead? A single forward pointer at position i can move chars[i] to its destination at n-1-i, but it has already overwritten whatever was at n-1-i — you need a temp variable and a second loop. Two pointers avoid this entirely: the swap is symmetric, so both elements land in their correct positions in one step, no temp array required.
Approach
- Set
left = 0,right = len(chars) - 1 - While
left < right:- Swap
chars[left]andchars[right] left += 1,right -= 1
- Swap
- Done — the array is reversed in-place
Solution
from typing import List
class Solution:
def flip_characters(self, chars: List[str]) -> None:
left = 0
right = len(chars) - 1
while left < right:
# Swap the mirror pair
chars[left], chars[right] = chars[right], chars[left]
# Move both pointers inward
left += 1
right -= 1
# --- Test ---
c1 = ['h', 'e', 'l', 'l', 'o']
Solution().flip_characters(c1)
print(c1) # ['o', 'l', 'l', 'e', 'h']
c2 = ['A', 'B', 'C', 'D']
Solution().flip_characters(c2)
print(c2) # ['D', 'C', 'B', 'A']
c3 = ['X']
Solution().flip_characters(c3)
print(c3) # ['X']
Dry Run — Example 1
chars = ['h', 'e', 'l', 'l', 'o'], n = 5
| Iteration | left | right | Swap | Array after swap |
|---|---|---|---|---|
| 1 | 0 | 4 | 'h' ↔ 'o' | ['o','e','l','l','h'] |
| 2 | 1 | 3 | 'e' ↔ 'l' | ['o','l','l','e','h'] |
| — | 2 | 2 | left ≥ right — stop | ['o','l','l','e','h'] ✓ |
The middle element at index 2 ('l') is its own mirror — no swap needed.
Complexity Analysis
| Complexity | Reasoning | |
|---|---|---|
| Time | O(n) | Each character is visited once; left and right together make n/2 swaps |
| Space | O(1) | Only two pointer variables — no auxiliary array |
Edge Cases
| Scenario | Input | Output | Note |
|---|---|---|---|
| Empty array | [] | [] | left = 0 > right = -1 — loop never runs |
| Single character | ['A'] | ['A'] | left = right = 0 — loop never runs |
| Two characters | ['A','B'] | ['B','A'] | One swap, then left = right = 1 — stops |
| Even length | ['A','B','C','D'] | ['D','C','B','A'] | All pairs swapped, no middle element |
| Odd length | ['A','B','C'] | ['C','B','A'] | Two pairs swapped, middle 'B' unchanged |
Key Takeaway
Flip Characters is the two-pointer reversal pattern applied to a character array. The mechanics are identical to reversing integers — the only difference is the element type. Every future problem in this section is a variation on this same core swap-and-converge idea.