-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDISCOUNTEDPRICESSTRIGN.java
More file actions
46 lines (45 loc) · 1.5 KB
/
DISCOUNTEDPRICESSTRIGN.java
File metadata and controls
46 lines (45 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public String discountPrices(String sentence, int discount) {
String[] arr = sentence.split(" ");
ArrayList<String> list = newa ArrayList<>();
for (String str : arr) {
if (str.charAt(0) == '$' && str.length() > 1 && isValidStrHai(str.substring(1))) {
list.add(str);
}
}
ArrayList<String> converted = new ArrayList<>();
for (String str : list) {
String convert = getDiscountedPrice(str, discount);
converted.add(convert);
}
StringBuilder sb = new StringBuilder();
int ind = 0;
for (String str : arr) {
if (str.charAt(0) == '$' && str.length() > 1 && isValidStrHai(str.substring(1))) {
sb.append(converted.get(ind++));
} else {
sb.append(str);
}
sb.append(" ");
}
return sb.toString().trim();
}
public static String getDiscountedPrice(String str, int discount) {
long num = 0;
int index = 1;
while (index < str.length()) {
num = (num * 10) + (str.charAt(index) - '0');
index++;
}
double discounted = num * (100 - discount) / 100.0;
return "$" + String.format("%.2f", discounted);
}
public static boolean isValidStrHai(String str) {
for (char c : str.toCharArray()) {
if (!Character.isDigit(c)) {
return false;
}
}
return true;
}
}