Transform a String with at Most One Operation
Updated
Problem statement
Determine whether s can become t using at most one of these operations:
- Insert one lowercase letter at any position in s, including either end.
- Replace one character of s with a lowercase letter.
- Swap the characters at any two distinct positions in s; the positions need not be adjacent.
You may also use no operation. Deletion is not allowed. Return true exactly when the transformation is possible.
Function
canTransformOnce(s: String, t: String) → booleanExamples
Example 1
s = "abcd"t = "dbca"return = trueSwap positions 0 and 3 in abcd to obtain dbca in one operation.
Example 2
s = "abc"t = "abxc"return = trueInsert x between b and c.
Example 3
s = "abc"t = "ac"return = falseThe target is shorter, but deletion is not an allowed operation.
Constraints
- 0 <= s.length <= 100000.
- 0 <= t.length <= 100000.
- s and t contain only lowercase English letters.
More LSEG Online Assessment questions
- Count Times Using at Most Two DigitsOA · Seen Jul 2025
- Maximum Sum of Exactly Three Non-Adjacent ElementsOA · Seen Jul 2025
- Maximum Word Count in a SentenceOA · Seen Jul 2025
- Minimum Digit Swaps for the Smallest DifferenceOA · Seen Jul 2025
- Smallest String After at Most One DeletionOA · Seen Jul 2025
- Delete One Digit 5 to Maximize the NumberOA · Seen Feb 2025
- Three-Part Partition with Minimum Largest RangeOA · Seen Feb 2025
- Price Equity Options and ForwardsOA · Seen Dec 2024
- Maximum Sum of Non-Adjacent ElementsOA · Seen Sep 2024
- Return the Sentence with the Most WordsOA · Seen Sep 2024
Similar questions at other companies
- Rearrange String by Alternating EndsHudson River Trading · OAHudson River Trading interview questions
- Alternate String EndsCapital One · OACapital One interview questions
- Run-Length String CompressionSalesforce · OASalesforce interview questions
- Minimum Changes for Palindromic BlocksCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Count Binary SubstringsMicrosoft · OAMicrosoft interview questions
- Check a Repeated String as a SubsequenceQuince · OAQuince interview questions
- Reverse String In PlaceThe Walt Disney Company · OA · PHONE SCREENThe Walt Disney Company interview questions
- Lost MessagesSuperhuman · OASuperhuman interview questions
- Reverse a StringFlexTrade · OAFlexTrade interview questions
- Missing WordsVanguard · OAVanguard interview questions