476. First Sale of a Product Sold Once
First Sale of a Product Sold Once

A shop records its daily sales in a string called salesLog. Each letter identifies a product type, and each occurrence represents one unit sold. Sales appear in the order they happened.

Find the first sale of a product that was sold exactly once during the entire day.

Method Signature

int firstSingleSaleIndex(String salesLog)
  • salesLog: the day's sales in chronological order.
  • Return the zero-based index of the first qualifying sale, or -1 if none exists.

Constraints

  • 1 ≤ salesLog.length() ≤ 100,000
  • salesLog contains only lowercase English letters.

Examples

Example 1

firstSingleSaleIndex(salesLog = "tggv")
Output: 0

Products t and v were each sold once. The sale of t appears first, at index 0.

Example 2

firstSingleSaleIndex(salesLog = "ppqrqsr")
Output: 5

Only product s was sold once. Its sale is at index 5.

Example 3

firstSingleSaleIndex(salesLog = "xyyxy")
Output: -1

Both product types were sold more than once.



Please use Laptop/Desktop or any other large screen to add/edit code.