Skip to content

Commit e84e665

Browse files
authored
Create Apply Operations to Make String Empty.java
1 parent 6a44f83 commit e84e665

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public String lastNonEmptyString(String s) {
3+
Map<Character, Integer> map = new HashMap<>();
4+
int maxFrequency = 0;
5+
for (int i = 0; i < s.length(); i++) {
6+
map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
7+
maxFrequency = Math.max(maxFrequency, map.get(s.charAt(i)));
8+
}
9+
StringBuilder result = new StringBuilder();
10+
for (int i = s.length() - 1; i >= 0; i--) {
11+
if (map.get(s.charAt(i)) == maxFrequency) {
12+
result.append(s.charAt(i));
13+
map.put(s.charAt(i), map.get(s.charAt(i)) - 1);
14+
}
15+
}
16+
return result.reverse().toString();
17+
}
18+
}

0 commit comments

Comments
 (0)