BackmediumStringsPhonePe

Ingredient Quantity Parser Solution

Problem Statement

Given a string of comma-separated ingredient-quantity pairs, where each ingredient is followed by its quantity, write a function to parse this input string and return a dictionary mapping each ingredient to its corresponding quantity.

Example 1
Input
apple,3,banana,2
Output
{'apple': 3, 'banana': 2}

Explanation: Step-by-step: 1. Split the input string into pairs using comma as delimiter. 2. For each pair, split into ingredient and quantity. 3. Try to parse the quantity as an integer. 4. If successful, add the ingredient-quantity pair to the dictionary.

Example 2
Input
banana,2,apple,2
Output
{'banana': 2, 'apple': 2}

Explanation: Step-by-step: 1. Split the input string into pairs using comma as delimiter. 2. For each pair, split into ingredient and quantity. 3. Try to parse the quantity as an integer. 4. If successful, add the ingredient-quantity pair to the dictionary.

Constraints

  • 1 <= length of input string <= 100
  • Ingredient quantities are between 1 and 9
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Ingredient Quantity Parser — Problem Statement & Solution Guide

StringsMediumMixed
TimeO(n)
|
SpaceO(n)

Problem Description

Given a string of comma-separated ingredient-quantity pairs, where each ingredient is followed by its quantity, write a function to parse this input string and return a dictionary mapping each ingredient to its corresponding quantity.

Examples

Example 1

Input

apple,3,banana,2

Output

{'apple': 3, 'banana': 2}

Explanation: Step-by-step: 1. Split the input string into pairs using comma as delimiter. 2. For each pair, split into ingredient and quantity. 3. Try to parse the quantity as an integer. 4. If successful, add the ingredient-quantity pair to the dictionary.

Example 2

Input

banana,2,apple,2

Output

{'banana': 2, 'apple': 2}

Explanation: Step-by-step: 1. Split the input string into pairs using comma as delimiter. 2. For each pair, split into ingredient and quantity. 3. Try to parse the quantity as an integer. 4. If successful, add the ingredient-quantity pair to the dictionary.

Constraints

  • 1 <= length of input string <= 100
  • Ingredient quantities are between 1 and 9

Optimal Approach & Strategy

The optimal approach involves parsing the input string and using the quantity to repeat the character in the output string. This can be achieved by iterating over the input string, separating characters from their quantities, and using a loop to repeat the character. This approach has a linear time complexity.

Brute Force Approach

The brute-force approach involves parsing the input string character by character, repeating each character by its quantity, and appending it to the result string. This approach has a high time complexity due to the repetitive string concatenation. It can be simplified by using a more efficient method to repeat characters.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function parseIngredients(input) {
  const ingredients = input.split(',');
  const result = {};
  for (let i = 0; i < ingredients.length; i += 2) {
    const ingredient = ingredients[i];
    const quantity = parseInt(ingredients[i + 1], 10);
    if (!isNaN(quantity)) {
      result[ingredient] = quantity;
    }
  }
  return result;
}

Asked in Top Tech Interviews

PhonePe

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.