Skip to content

Commit f09270b

Browse files
S
1 parent 84f4363 commit f09270b

File tree

8 files changed

+276
-0
lines changed

8 files changed

+276
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Solution {
2+
public int AreaOfMaxDiagonal(int[][] dimensions) {
3+
int ans = 0, mx = 0;
4+
foreach (var d in dimensions) {
5+
int l = d[0], w = d[1];
6+
int t = l * l + w * w;
7+
if (mx < t) {
8+
mx = t;
9+
ans = l * w;
10+
} else if (mx == t) {
11+
ans = Math.Max(ans, l * w);
12+
}
13+
}
14+
return ans;
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
pub fn area_of_max_diagonal(dimensions: Vec<Vec<i32>>) -> i32 {
3+
let mut ans = 0;
4+
let mut mx = 0;
5+
for d in dimensions {
6+
let l = d[0];
7+
let w = d[1];
8+
let t = l * l + w * w;
9+
if mx < t {
10+
mx = t;
11+
ans = l * w;
12+
} else if mx == t {
13+
ans = ans.max(l * w);
14+
}
15+
}
16+
ans
17+
}
18+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
---
5+
6+
<!-- problem:start -->
7+
8+
# [3662. Filter Characters by Frequency 🔒](https://leetcode.com/problems/filter-characters-by-frequency)
9+
10+
[中文文档](/solution/3600-3699/3662.Filter%20Characters%20by%20Frequency/README.md)
11+
12+
## Description
13+
14+
<!-- description:start -->
15+
16+
<p>You are given a string <code>s</code> consisting of lowercase English letters and an integer <code>k</code>.</p>
17+
18+
<p>Your task is to construct a new string that contains only those characters from <code>s</code> which appear <strong>fewer</strong> than <code>k</code> times in the entire string. The order of characters in the new string must be the <strong>same</strong> as their <strong>order</strong> in <code>s</code>.</p>
19+
20+
<p>Return the resulting string. If no characters qualify, return an empty string.</p>
21+
22+
<p>Note: <strong>Every occurrence</strong> of a character that occurs fewer than <code>k</code> times is kept.</p>
23+
24+
<p>&nbsp;</p>
25+
<p><strong class="example">Example 1:</strong></p>
26+
27+
<div class="example-block">
28+
<p><strong>Input:</strong> <span class="example-io">s = &quot;aadbbcccca&quot;, k = 3</span></p>
29+
30+
<p><strong>Output:</strong> <span class="example-io">&quot;dbb&quot;</span></p>
31+
32+
<p><strong>Explanation:</strong></p>
33+
34+
<p>Character frequencies in <code>s</code>:</p>
35+
36+
<ul>
37+
<li><code>&#39;a&#39;</code> appears 3 times</li>
38+
<li><code>&#39;d&#39;</code> appears 1 time</li>
39+
<li><code>&#39;b&#39;</code> appears 2 times</li>
40+
<li><code>&#39;c&#39;</code> appears 4 times</li>
41+
</ul>
42+
43+
<p>Only <code>&#39;d&#39;</code> and <code>&#39;b&#39;</code> appear fewer than 3 times. Preserving their order, the result is <code>&quot;dbb&quot;</code>.</p>
44+
</div>
45+
46+
<p><strong class="example">Example 2:</strong></p>
47+
48+
<div class="example-block">
49+
<p><strong>Input:</strong> <span class="example-io">s = &quot;xyz&quot;, k = 2</span></p>
50+
51+
<p><strong>Output:</strong> <span class="example-io">&quot;xyz&quot;</span></p>
52+
53+
<p><strong>Explanation:</strong></p>
54+
55+
<p>All characters (<code>&#39;x&#39;</code>, <code>&#39;y&#39;</code>, <code>&#39;z&#39;</code>) appear exactly once, which is fewer than 2. Thus the whole string is returned.</p>
56+
</div>
57+
58+
<p>&nbsp;</p>
59+
<p><strong>Constraints:</strong></p>
60+
61+
<ul>
62+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
63+
<li><code>s</code> consists of lowercase English letters.</li>
64+
<li><code>1 &lt;= k &lt;= s.length</code></li>
65+
</ul>
66+
67+
<!-- description:end -->
68+
69+
## Solutions
70+
71+
<!-- solution:start -->
72+
73+
### Solution 1: Counting
74+
75+
First, we iterate through the string $s$ and count the frequency of each character, storing the results in a hash table or array $\textit{cnt}$.
76+
77+
Then, we iterate through the string $s$ again, adding characters whose frequency is less than $k$ to the result string. Finally, we return the result string.
78+
79+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(|\Sigma|)$, where $\Sigma$ is the size of the character set.
80+
81+
<!-- tabs:start -->
82+
83+
#### Python3
84+
85+
```python
86+
class Solution:
87+
def filterCharacters(self, s: str, k: int) -> str:
88+
cnt = Counter(s)
89+
ans = []
90+
for c in s:
91+
if cnt[c] < k:
92+
ans.append(c)
93+
return "".join(ans)
94+
```
95+
96+
#### Java
97+
98+
```java
99+
class Solution {
100+
public String filterCharacters(String s, int k) {
101+
int[] cnt = new int[26];
102+
for (char c : s.toCharArray()) {
103+
++cnt[c - 'a'];
104+
}
105+
StringBuilder ans = new StringBuilder();
106+
for (char c : s.toCharArray()) {
107+
if (cnt[c - 'a'] < k) {
108+
ans.append(c);
109+
}
110+
}
111+
return ans.toString();
112+
}
113+
}
114+
```
115+
116+
#### C++
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
string filterCharacters(string s, int k) {
122+
int cnt[26]{};
123+
for (char c : s) {
124+
++cnt[c - 'a'];
125+
}
126+
string ans;
127+
for (char c : s) {
128+
if (cnt[c - 'a'] < k) {
129+
ans.push_back(c);
130+
}
131+
}
132+
return ans;
133+
}
134+
};
135+
```
136+
137+
#### Go
138+
139+
```go
140+
func filterCharacters(s string, k int) string {
141+
cnt := [26]int{}
142+
for _, c := range s {
143+
cnt[c-'a']++
144+
}
145+
ans := []rune{}
146+
for _, c := range s {
147+
if cnt[c-'a'] < k {
148+
ans = append(ans, c)
149+
}
150+
}
151+
return string(ans)
152+
}
153+
```
154+
155+
#### TypeScript
156+
157+
```ts
158+
function filterCharacters(s: string, k: number): string {
159+
const cnt: Record<string, number> = {};
160+
for (const c of s) {
161+
cnt[c] = (cnt[c] || 0) + 1;
162+
}
163+
const ans: string[] = [];
164+
for (const c of s) {
165+
if (cnt[c] < k) {
166+
ans.push(c);
167+
}
168+
}
169+
return ans.join('');
170+
}
171+
```
172+
173+
<!-- tabs:end -->
174+
175+
<!-- solution:end -->
176+
177+
<!-- problem:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
string filterCharacters(string s, int k) {
4+
int cnt[26]{};
5+
for (char c : s) {
6+
++cnt[c - 'a'];
7+
}
8+
string ans;
9+
for (char c : s) {
10+
if (cnt[c - 'a'] < k) {
11+
ans.push_back(c);
12+
}
13+
}
14+
return ans;
15+
}
16+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func filterCharacters(s string, k int) string {
2+
cnt := [26]int{}
3+
for _, c := range s {
4+
cnt[c-'a']++
5+
}
6+
ans := []rune{}
7+
for _, c := range s {
8+
if cnt[c-'a'] < k {
9+
ans = append(ans, c)
10+
}
11+
}
12+
return string(ans)
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public String filterCharacters(String s, int k) {
3+
int[] cnt = new int[26];
4+
for (char c : s.toCharArray()) {
5+
++cnt[c - 'a'];
6+
}
7+
StringBuilder ans = new StringBuilder();
8+
for (char c : s.toCharArray()) {
9+
if (cnt[c - 'a'] < k) {
10+
ans.append(c);
11+
}
12+
}
13+
return ans.toString();
14+
}
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def filterCharacters(self, s: str, k: int) -> str:
3+
cnt = Counter(s)
4+
ans = []
5+
for c in s:
6+
if cnt[c] < k:
7+
ans.append(c)
8+
return "".join(ans)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function filterCharacters(s: string, k: number): string {
2+
const cnt: Record<string, number> = {};
3+
for (const c of s) {
4+
cnt[c] = (cnt[c] || 0) + 1;
5+
}
6+
const ans: string[] = [];
7+
for (const c of s) {
8+
if (cnt[c] < k) {
9+
ans.push(c);
10+
}
11+
}
12+
return ans.join('');
13+
}

0 commit comments

Comments
 (0)