Problem Β· Sorting

Sort Product Codes πŸ‘

Learn this problem
● EasyAmazonINTERNOA
See Amazon hiring insights

Problem statement

Amazon has millions of products sold on its e-commerce website, and each product is identified by its product code.

Given an array of n productCodes and order, a string that represents the precedence of characters, sort the productCodes in lexicographically increasing order per the precedence.

Note: Lexicographical order is defined in the following way. When we compare strings s and t, first we find the leftmost position with differing characters: sl and tl. If there is no such position (i.e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters sl and t[i] according to their order in the given precedence order.

Function

sortProductCodes(order: String, productCodes: String[]) β†’ String[]

Complete the function sortProductCodes in the editor below. sortProductCodes has the following parameter(s):

  • string order: the new precedence order string
  • productCodes[n]: the array to sort

Returns

string[n]: the productCodes array in sorted order

Examples

Example 1

order = "abcdefghijklmnopqrstuvwxyz"productCodes = ["adc", "abc"]return = ["abc", "adc"]
Consider the strings "adc" and "abc", the first point of difference is at position 1 (assuming start index is 0), and 'd'>'b' according to the given precedence order.

Constraints

  • 1 ≤ n ≤ 5000
  • 1 ≤ length(productCodes[i]) ≤ 100
  • length(order) = 26
  • order and all productCodes[i] contain lowercase English letters only.
  • More Amazon problems

    drafts saved locally
    public String[] sortProductCodes(String order, String[] productCodes) {
        // write your code here
    }
    
    order"abcdefghijklmnopqrstuvwxyz"
    productCodes["adc", "abc"]
    expected["abc", "adc"]
    checking account