Problem · String

Equal Products

Learn this problem
MediumAmazonOA
See Amazon hiring insights

Problem statement

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.

    Function

    equalProducts(inventory_alpha: String[], inventory_beta: String[]) → int[]

    Examples

    Example 1

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

    Example 2

    inventory_alpha = ["azzel"]inventory_beta = ["apple"]return = [1]
    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

    • 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

    More Amazon problems

    drafts saved locally
    public int[] equalProducts(String[] inventory_alpha, String[] inventory_beta) {
      // write your code here
    }
    
    inventory_alpha["abba"]
    inventory_beta["adda"]
    expected[1]
    checking account