LeetCode in Kotlin

2288. Apply Discount to Prices

Medium

A sentence is a string of single-space separated words where each word can contain digits, lowercase letters, and the dollar sign '$'. A word represents a price if it is a sequence of digits preceded by a dollar sign.

You are given a string sentence representing a sentence and an integer discount. For each word representing a price, apply a discount of discount% on the price and update the word in the sentence. All updated prices should be represented with exactly two decimal places.

Return a string representing the modified sentence.

Note that all prices will contain at most 10 digits.

Example 1:

Input: sentence = “there are $1 $2 and 5$ candies in the shop”, discount = 50

Output: “there are $0.50 $1.00 and 5$ candies in the shop”

Explanation:

The words which represent prices are “$1” and “$2”.

Example 2:

Input: sentence = “1 2 $3 4 $5 $6 7 8$ $9 $10$”, discount = 100

Output: “1 2 $0.00 4 $0.00 $0.00 7 8$ $0.00 $10$”

Explanation:

Applying a 100% discount on any price will result in 0.

The words representing prices are “$3”, “$5”, “$6”, and “$9”.

Each of them is replaced by “$0.00”.

Constraints:

Solution

class Solution {
    fun discountPrices(sentence: String, discount: Int): String {
        val words = sentence.split(" ").dropLastWhile { it.isEmpty() }.toTypedArray()
        val sb = StringBuilder()
        for (word in words) {
            sb.append(applyDiscount(word, discount))
            sb.append(" ")
        }
        sb.deleteCharAt(sb.length - 1)
        return sb.toString()
    }

    private fun applyDiscount(s: String, discount: Int): String {
        if (s[0] == '$' && s.length > 1) {
            var price: Long = 0
            for (i in 1 until s.length) {
                if (!Character.isDigit(s[i])) {
                    // Error case. We could also use Long.parseLong() here.
                    return s
                }
                price *= 10
                price += ((s[i].code - '0'.code) * (100 - discount)).toLong()
            }
            val stringPrice = price.toString()
            if (price < 10) {
                return "$0.0$stringPrice"
            }
            return if (price < 100) {
                "$0.$stringPrice"
            } else (
                "$" +
                    stringPrice.substring(0, stringPrice.length - 2) +
                    "." +
                    stringPrice.substring(stringPrice.length - 2)
                )
        }
        return s
    }
}