Ingredient Quantity Parser — Problem Statement & Solution Guide
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
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.
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
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;
}class Solution {
public java.util.HashMap<String, Integer> ingredientQuantityParser(String s) {
java.util.HashMap<String, Integer> result = new java.util.HashMap<>();
String[] pairs = s.split(',');
for (int i = 0; i < pairs.length; i += 2) {
String ingredient = pairs[i];
String quantity = pairs[i + 1];
try {
result.put(ingredient, Integer.parseInt(quantity));
} catch (NumberFormatException e) {
// Ignore invalid quantities
}
}
return result;
}
}def ingredient_quantity_parser(s):
result = {}
pairs = s.split(',')
for i in range(0, len(pairs), 2):
ingredient = pairs[i]
quantity = pairs[i + 1]
try:
result[ingredient] = int(quantity)
except ValueError:
pass
return resultfunction 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
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.