FastPrepCheck if One String Swap Can Make Strings Equal

Check if One String Swap Can Make Strings Equal

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Given two strings first and second of equal length, return true if they are already equal or can be made equal by swapping exactly two positions in first. Otherwise return false.

Function

areAlmostEqual(first: String, second: String) → boolean

Examples

Example 1

first = "bank"second = "kanb"return = true

Swapping indices 0 and 3 in first produces second.

Example 2

first = "attack"second = "defend"return = false

More than two positions differ.

Constraints

  • 1 <= first.length == second.length <= 10^5.
  • Both strings contain lowercase English letters.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public boolean areAlmostEqual(String first, String second) {
    // Write your code here.
}
first"bank"
second"kanb"
expectedtrue
Checking account…