|
56 | 56 | <!-- tabs:start -->
|
57 | 57 |
|
58 | 58 | ```python
|
59 |
| - |
| 59 | +class Solution: |
| 60 | + def lastNonEmptyString(self, s: str) -> str: |
| 61 | + cnt = Counter(s) |
| 62 | + mx = cnt.most_common(1)[0][1] |
| 63 | + last = {c: i for i, c in enumerate(s)} |
| 64 | + return "".join(c for i, c in enumerate(s) if cnt[c] == mx and last[c] == i) |
60 | 65 | ```
|
61 | 66 |
|
62 | 67 | ```java
|
63 |
| - |
| 68 | +class Solution { |
| 69 | + public String lastNonEmptyString(String s) { |
| 70 | + int[] cnt = new int[26]; |
| 71 | + int[] last = new int[26]; |
| 72 | + int n = s.length(); |
| 73 | + int mx = 0; |
| 74 | + for (int i = 0; i < n; ++i) { |
| 75 | + int c = s.charAt(i) - 'a'; |
| 76 | + mx = Math.max(mx, ++cnt[c]); |
| 77 | + last[c] = i; |
| 78 | + } |
| 79 | + StringBuilder ans = new StringBuilder(); |
| 80 | + for (int i = 0; i < n; ++i) { |
| 81 | + int c = s.charAt(i) - 'a'; |
| 82 | + if (cnt[c] == mx && last[c] == i) { |
| 83 | + ans.append(s.charAt(i)); |
| 84 | + } |
| 85 | + } |
| 86 | + return ans.toString(); |
| 87 | + } |
| 88 | +} |
64 | 89 | ```
|
65 | 90 |
|
66 | 91 | ```cpp
|
67 |
| - |
| 92 | +class Solution { |
| 93 | +public: |
| 94 | + string lastNonEmptyString(string s) { |
| 95 | + int cnt[26]{}; |
| 96 | + int last[26]{}; |
| 97 | + int n = s.size(); |
| 98 | + int mx = 0; |
| 99 | + for (int i = 0; i < n; ++i) { |
| 100 | + int c = s[i] - 'a'; |
| 101 | + mx = max(mx, ++cnt[c]); |
| 102 | + last[c] = i; |
| 103 | + } |
| 104 | + string ans; |
| 105 | + for (int i = 0; i < n; ++i) { |
| 106 | + int c = s[i] - 'a'; |
| 107 | + if (cnt[c] == mx && last[c] == i) { |
| 108 | + ans.push_back(s[i]); |
| 109 | + } |
| 110 | + } |
| 111 | + return ans; |
| 112 | + } |
| 113 | +}; |
68 | 114 | ```
|
69 | 115 |
|
70 | 116 | ```go
|
| 117 | +func lastNonEmptyString(s string) string { |
| 118 | + cnt := [26]int{} |
| 119 | + last := [26]int{} |
| 120 | + mx := 0 |
| 121 | + for i, c := range s { |
| 122 | + c -= 'a' |
| 123 | + cnt[c]++ |
| 124 | + last[c] = i |
| 125 | + mx = max(mx, cnt[c]) |
| 126 | + } |
| 127 | + ans := []rune{} |
| 128 | + for i, c := range s { |
| 129 | + if cnt[c-'a'] == mx && last[c-'a'] == i { |
| 130 | + ans = append(ans, c) |
| 131 | + } |
| 132 | + } |
| 133 | + return string(ans) |
| 134 | +} |
| 135 | +``` |
71 | 136 |
|
| 137 | +```ts |
| 138 | +function lastNonEmptyString(s: string): string { |
| 139 | + const cnt: number[] = Array(26).fill(0); |
| 140 | + const last: number[] = Array(26).fill(0); |
| 141 | + const n = s.length; |
| 142 | + let mx = 0; |
| 143 | + for (let i = 0; i < n; ++i) { |
| 144 | + const c = s.charCodeAt(i) - 97; |
| 145 | + mx = Math.max(mx, ++cnt[c]); |
| 146 | + last[c] = i; |
| 147 | + } |
| 148 | + const ans: string[] = []; |
| 149 | + for (let i = 0; i < n; ++i) { |
| 150 | + const c = s.charCodeAt(i) - 97; |
| 151 | + if (cnt[c] === mx && last[c] === i) { |
| 152 | + ans.push(String.fromCharCode(c + 97)); |
| 153 | + } |
| 154 | + } |
| 155 | + return ans.join(''); |
| 156 | +} |
72 | 157 | ```
|
73 | 158 |
|
74 | 159 | <!-- tabs:end -->
|
|
0 commit comments