Apply Discount to Prices LeetCode solution in Java

1
class Solution {
2

3
public String discountPrices(String sentence, int discount) {
4
String x[] = sentence.split(" ");
5
StringBuilder sb = new StringBuilder();
6
for (String s : x) {
7
if (isPrice(s)) sb.append(calc(Double.parseDouble(s.substring(1)), discount) + " ");
8
else sb.append(s + " ");
9
}
10
sb.deleteCharAt(sb.length() - 1);
11
return sb.toString();
12
}
13

14
boolean isPrice(String s) {
15
return s.startsWith("$") && s.substring(1).matches("\\d+");
16
}
17

18
String calc(double num, double discount) {
19
double ans = num - (double) ((double) num * discount / 100.00);
20
return "$" + String.format("%.2f", ans);
21
}
22
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0