Amazon | OA | Discount Coupons
Anonymous User
1008

A discount coupon ([a-z]) is valid if it follows one of the 3 conditions:

  1. An empty string is a valid coupon
  2. A valid coupon with x on either side of it is also valid (xAx is a valid coupon)
  3. A concatentation of 2 valid coupons A and B is a also valid coupon (AB & BA are both valid)

Input: []string of discount codes
Output: []int32 with 1 at ith position if the discount code ith i valid otherwise 0

Example:

Input := []string{"abba", "abc", "daaccd"}
Output := []int32{1, 0, 1}

// `abba`: With Rule 1 empty string is valid. 
//     Adding `b` to both size keeps it valid by rule 2, 
//     Similarly adding `a` on both sides keeps it valid

// `abc`: It's not valid by any rule

// `daaccd`: `aa` is valid by rule 1 and so is `cc`. 
//      Concatenation of them is valid by rule 3 and 
//      adding `d` to either side keeps it valid by rule 1
Comments (2)