Problem · String
Reverse Letters in Pairs
Learn this problemProblem statement
Given a string s of lowercase English letters, reverse its letters in adjacent pairs.
For every pair of positions (0,1), (2,3), and so on, place the second letter before the first. If the string has odd length, leave its final letter in the same position.
Return the resulting string.
Function
reverseLetterPairs(s: String) → StringExamples
Example 1
s = "abcdef"return = "badcfe"The pairs ab, cd, and ef become ba, dc, and fe.
Example 2
s = "abcde"return = "badce"The pairs ab and cd are reversed. The unpaired final letter e stays in place.
Example 3
s = "z"return = "z"A one-letter string has no complete pair, so it is unchanged.
Constraints
1 <= s.length <= 100scontains only lowercase English letters.
More Tiktok problems
- Concatenate Digit-wise SumsOA · Seen Aug 2026
- Count 2x2 Submatrices by Black CellsOA · Seen Aug 2026
- Count House Segments After DestructionOA · Seen Aug 2026
- Debugger Breakpoint ActionsOA · Seen Aug 2026
- Distribution Center Package AllocationOA · Seen Aug 2026
- Find All Local PeaksOA · Seen Aug 2026
- Minimum Height Difference Between Distant PeaksOA · Seen Aug 2026
- Minimum Operations for Stepwise StructuresOA · Seen Aug 2026