NewProject Coding is here — build features and fix bugs in real codebases with an AI assistantSee what's new

A shopping cart contains product records stored as strings. You are given items, where a valid record has exactly four pipe-delimited fields: "<sku>|<productName>|<quantity>|<unitPriceCents>". The sku (stock-keeping unit) is a product identifier. The quantity is the number of units purchased, and the unit price is in cents.

Ignore a record if a required field is missing, its SKU or product name is empty, its quantity or unit price is not an integer, or either number is negative. Sum the quantities and total cost of all valid records.

Return a string formatted as "total unit=<quantity>, total price=$<amount>", with exactly two digits after the decimal point.

Constraints:

  • 1 ≤ items.length ≤ 1000.
  • Each valid quantity and unit price is at most 10,000.

Example 1:

Input: items = ["a100|Apple|7|125", "b200|Banana|3|200", "bad||2|100", "c300|Cake|-1|500"] Output: "total unit=10, total price=$14.75" Explanation: The record with an empty product name and the record with a negative quantity are ignored. The two valid records contain 7 + 3 = 10 units and cost 7 × 125 + 3 × 200 = 1475 cents ($14.75).

Example 2:

Input: items = ["x|X|2|150", "y|Y|1|275"] Output: "total unit=3, total price=$5.75"

Example 3:

Input: items = ["a|Apple|4|125", "b|Bread|2|349", "c|Milk|1|250"] Output: "total unit=7, total price=$14.48"


Accepted
60/292
Acceptance Rate20.5%
Test Cases
Input
items =
Output
Expected