Reverse Letters in Pairs
Problem 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.