Problem · String

Reverse Letters in Pairs

Learn this problem
EasyTiktok logoTiktokINTERNOA
See Tiktok hiring insights

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) → String

Examples

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 <= 100
  • s contains only lowercase English letters.

More Tiktok problems

drafts saved locally
public String reverseLetterPairs(String s) {
    // Write your code here.
}
s"abcdef"
expected"badcfe"
checking account