8000 solved 3223 · realtemirov/leetcode@f8d6e3b · GitHub
[go: up one dir, main page]

Skip to content

Commit f8d6e3b

Browse files
committed
solved 3223
1 parent 1f17dc0 commit f8d6e3b

File tree

6 files changed

+104
-2
lines changed

6 files changed

+104
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 3223. Minimum Length of String After Operations
2+
3+
🟧 Medium
4+
5+
## Solution
6+
7+
My Solution
8+
9+
```go
10+
func minimumLength(s string) int {
11+
if len(s) < 3 {
12+
return len(s)
13+
}
14+
15+
m := [26]int{}
16+
17+
for _, c := range s {
18+
m[c-'a']++
19+
}
20+
21+
sum := 0
22+
for _, val := range m {
23+
if val == 0 {
24+
continue
25+
}
26+
if val&1 == 1 {
27+
sum += 1
28+
} else {
29+
sum += 2
30+
}
31+
}
32+
33+
return sum
34+
}
35+
```
36+
37+
![result](3223.png)
38+
39+
Leetcode: [link](https://leetcode.com/problems/minimum-length-of-string-after-operations/description/)

Problems/3223/3223.png

33.7 KB
Loading

Problems/3223/solution.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package problem3223
2+
3+
func MinimumLength(s string) int {
4+
if len(s) < 3 {
5+
return len(s)
6+
}
7+
8+
m := [26]int{}
9+
10+
for _, c := range s {
11+
m[c-'a']++
12+
}
13+
14+
sum := 0
15+
for _, val := range m {
16+
if val == 0 {
17+
continue
18+
}
19+
if val&1 == 1 {
20+
sum += 1
21+
} else {
22+
sum += 2
23+
}
24+
}
25+
26+
return sum
27+
}

Problems/3223/solution_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package problem3223_test
2+
3+
import (
4+
"testing"
5+
6+
problem3223 "github.com/realtemirov/leetcode/Problems/3223"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestSolution(t *testing.T) {
11+
testCases := []struct {
12+
name string
13+
cases string
14+
expected int
15+
}{
16+
{
17+
name: "Test 1",
18+
cases: "abaacbcbb",
19+
expected: 5,
20+
},
21+
{
22+
name: "Test 2",
23+
cases: "aa",
24+
expected: 2,
25+
},
26+
}
27+
28+
for _, tc := range testCases {
29+
t.Run(tc.name, func(t *testing.T) {
30+
result := problem3223.MinimumLength(tc.cases)
31+
require.Equal(t, tc.expected, result, "expected: %v, result: %v", tc.expected, result)
32+
})
33+
}
34+
}

Problems/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Problems
22

3-
> **Total:** `75` \
3+
> **Total:** `76` \
44
> **Easy:** `53` \
5-
> **Medium:** `22` \
5+
> **Medium:** `23` \
66
> **Hard:** `0`
77
88
* 🟩 Easy - [1. Two Sum](<./0001/1. Two Sum.md>)
@@ -80,3 +80,4 @@
8080
* 🟧 Medium - [2381. Shifting Letters II](<./2381/2381. Shifting Letters II.md>)
8181
* 🟧 Medium - [2559. Count Vowel Strings in Ranges](<./2559/2559. Count Vowel Strings in Ranges.md>)
8282
* 🟩 Easy - [3042. Count Prefix and Suffix Pairs I](<./3042/3042. Count Prefix and Suffix Pairs I.md>)
83+
* 🟧 Medium - [3223. Minimum Length of String After Operations](<./3223/3223. Minimum Length of String After Operations.md>)

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
* [2381. Shifting Letters II](<./Problems/2381/2381. Shifting Letters II.md>)
136136
* [2559. Count Vowel Strings in Ranges](<./Problems/2559/2559. Count Vowel Strings in Ranges.md>)
137137
* [3042. Count Prefix and Suffix Pairs I](<./Problems/3042/3042. Count Prefix and Suffix Pairs I.md>)
138+
* [3223. Minimum Length of String After Operations](<./Problems/3223/3223. Minimum Length of String After Operations.md>)
138139
<!--
139140
140141
* [20. Valid Parentheses](<Problems/20. Valid Parentheses.md>)

0 commit comments

Comments
 (0)
0