FastPrepFastPrep
Problem Brief

Equal Products

OA

At one of Amazon’s busy warehouses, Team Alpha and Team Beta are assigned the task of processing n products for shipment. Each product handled by Team Alpha is labeled as inventory_alpha[i], and the corresponding product handled by Team Beta is labeled as inventory_beta[i].

For every product pair (inventory_alpha[i], inventory_beta[i]), Team Alpha is allowed to repeatedly perform the following operation on their product label:

  • Pick any two distinct lowercase alphabet letters char1 and char2.
  • Swap every occurrence of char1 with char2 throughout the entire string (and vice versa).
  • The chosen characters don’t both have to exist in the string — it’s valid if either char1 or char2 is missing.
  • Your task is to determine, for each product pair, whether Team Alpha can transform inventory_alpha[i] into inventory_beta[i] by performing the swap operation any number of times (including zero), while following these constraints:

  • For each index i where 1 ≤ i ≤ n, Team Alpha may repeatedly:
  • Select any two distinct lowercase letters ch1 and ch2.
  • Swap every occurrence of ch1 with ch2, and every occurrence of ch2 with ch1 within the string.
  • The original problem asks to return bool[], but I modified it to int[]. 1 represents true, while 0 represents false.

    1Example 1

    Input
    inventory_alpha = ["abba"], inventory_beta = ["adda"]
    Output
    1
    Explanation
    Example 1 illustration
    It is possible to transform "abba" to "adda" by replacing b with d. Hence our answer is 1!

    2Example 2

    Input
    inventory_alpha = ["azzel"], inventory_beta = ["apple"]
    Output
    1
    Explanation
    Swap every occurrence of 'z' with 'p', transforming "azzel" into "appel". Then replace 'l' with 'e' and 'e' with 'l'. After performing these swaps, inventory_alpha = "azzel" becomes identical to inventory_beta = "apple".

    Constraints

    Limits and guarantees your solution can rely on.

    • 1 ≤ n ≤ 10^5
    • 1 ≤ |inventory_alpha[i]|, |inventory_beta[i]| ≤ 2*10^5
    • |inventory_alpha[i]| = |inventory_beta[i]|
    • inventory_alpha[i] and inventory_beta[i] contain only lowercase Latin letters.
    • The sum of lengths of all strings in inventory_alpha and inventory_beta does not exceed 5 * 10^5
    public int[] equalProducts(String[] inventory_alpha, String[] inventory_beta) {
      // write your code here
    }
    
    Input

    inventory_alpha

    ["abba"]

    inventory_beta

    ["adda"]

    Output

    1

    Sign in to submit your solution.