Swap Adjacent Character Pairs
Updated
Problem statement
Given a string text, swap the two characters in every consecutive, nonoverlapping pair, starting from the beginning, and return the resulting string.
The pairs use zero-based positions (0, 1), (2, 3), and so on. If the length is odd, leave the final unpaired character unchanged. Preserve the order of the pairs.
For example, abcd becomes badc.
Function
swapAdjacentPairs(text: String) → StringExamples
Example 1
text = "abcd"return = "badc"Swap a with b, then c with d.
Example 2
text = "abcde"return = "badce"The pairs are swapped, while the final e has no partner and stays unchanged.
Example 3
text = "a"return = "a"There is no complete pair, so the string stays unchanged.
Constraints
1 <= text.length <= 10^5.textcontains only lowercase English lettersathroughz.
More Hartford Financial Services interview questions
Similar questions at other companies
- Christmas Tree with N TiersVerily · ONSITE INTERVIEW
- Longest Common PrefixKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Valid ParenthesesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Count Adjacent Symbol Pairs for BPEAmazon · ONSITE INTERVIEW
- Longest Spoken PrefixElevenLabs · ONSITE INTERVIEWElevenLabs interview questions
- Palindrome Check Ignoring SymbolsWalmart · ONSITE INTERVIEWWalmart interview questions
- Reverse a String Keeping Dashes in PlaceNykaa · ONSITE INTERVIEWNykaa interview questions
- Implement tail -nConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
- Valid ParenthesesEPAM · ONSITE INTERVIEWEPAM interview questions
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions