Apply Discount to Prices - Problem
Apply Discount to Prices
Imagine you're building a price comparison website that automatically applies discount coupons to all prices found in product descriptions! Your task is to identify valid prices in a sentence and apply a discount to them.
Given a sentence (string of space-separated words) and a discount percentage, you need to:
1️⃣ Identify valid prices: A price is a word that starts with
• ✅ Valid:
• ❌ Invalid:
2️⃣ Apply discount: Reduce each valid price by the given discount percentage
3️⃣ Format result: Display all updated prices with exactly 2 decimal places
Goal: Return the modified sentence with discounted prices formatted to 2 decimal places.
Imagine you're building a price comparison website that automatically applies discount coupons to all prices found in product descriptions! Your task is to identify valid prices in a sentence and apply a discount to them.
Given a sentence (string of space-separated words) and a discount percentage, you need to:
1️⃣ Identify valid prices: A price is a word that starts with
$ followed by only digits• ✅ Valid:
"$100", "$23", "$6"• ❌ Invalid:
"100" (no $), "$" (no digits), "$1e5" (contains 'e')2️⃣ Apply discount: Reduce each valid price by the given discount percentage
3️⃣ Format result: Display all updated prices with exactly 2 decimal places
Goal: Return the modified sentence with discounted prices formatted to 2 decimal places.
Input & Output
example_1.py — Basic Price Discount
$
Input:
sentence = "there are $1 items that cost $100 each", discount = 20
›
Output:
"there are $0.80 items that cost $80.00 each"
💡 Note:
Valid prices $1 and $100 get 20% discount: $1 becomes $0.80, $100 becomes $80.00. Non-price words remain unchanged.
example_2.py — Mixed Valid and Invalid Prices
$
Input:
sentence = "1 2 $3 4$ 5$ 6 7$ $8 $", discount = 0
›
Output:
"1 2 $3.00 4$ 5$ 6 7$ $8.00 $"
💡 Note:
Only $3 and $8 are valid prices ($ followed by only digits). With 0% discount, they become $3.00 and $8.00. Invalid formats like '4$', '5$', '7$', '$' remain unchanged.
example_3.py — Edge Case with Large Price
$
Input:
sentence = "buy $1000000000 worth of items", discount = 50
›
Output:
"buy $500000000.00 worth of items"
💡 Note:
Large valid price $1000000000 gets 50% discount, becoming $500000000.00 formatted to exactly 2 decimal places.
Visualization
Tap to expand
Understanding the Visualization
1
Text Scanner
Split the sentence into individual words, like scanning each item
2
Price Validator
Check if each word is a valid price: starts with $ and contains only digits
3
Discount Engine
Apply the discount percentage to valid prices using the formula
4
Format & Rebuild
Format prices to 2 decimals and reconstruct the sentence
Key Takeaway
🎯 Key Insight: Leverage optimized built-in string methods like `startswith()` and `isdigit()` for clean, efficient price validation instead of manual character-by-character checking.
Time & Space Complexity
Time Complexity
O(n*m)
n words, m average word length, but optimized with built-in methods
✓ Linear Growth
Space Complexity
O(n)
Space for word array and result string construction
⚡ Linearithmic Space
Constraints
- 1 ≤ sentence.length ≤ 105
-
sentence consists of lowercase English letters, digits,
' ', and'$' - sentence does not have leading or trailing spaces
- All words in sentence are separated by a single space
- 0 ≤ discount ≤ 100
- All prices will contain at most 10 digits
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code