Skip to content

Commit e42a320

Browse files
authored
Merge pull request #14 from LuaanNguyen/leetcode-680
680 + 344
2 parents c64e095 + 2d4008c commit e42a320

File tree

5 files changed

+116
-2
lines changed

5 files changed

+116
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ go clean -testcache
5757
| [167](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/) | [Two Sum II - Input Array Is Sorted](/medium/167_two_sum_II_input_array_is_sorted) | medium | _`Two Pointers`_ | We know that the array is sorted in asc order. We can track the total sum of 2 pointers left and right and increase or decrease their indices accordingly in a while loop |
5858
| [15](https://leetcode.com/problems/3sum/description/) | [3Sum](/medium/15_3sum) | medium | _`Two Pointers`_ | Interate through the array, initialize 2 other pointers j = i + 1 and k = n - 1 and check the sum and adjust j and k accordingly. Make sure to check duplicates |
5959
| [3375](https://leetcode.com/problems/minimum-operations-to-make-array-values-equal-to-k/description) | [3Sum](/easy/3375_minimum_operations_to_make_array_values_equal_to_k) | medium | _`Hashmap`_ | There is no solution if a value that smaller than k. If a value is larger than k, we add it to our set and count the length of the set at the end |
60+
| [680](https://leetcode.com/problems/valid-palindrome-ii/description/) | [Valid Palindrome II](/easy/680_valid_palindrome_II) | easy | _`Two Pointers`_ | Have a helper function which has the same structure as the main function. Use 2 pointers to check the +1 or -1 element whether they are equal or not |
61+
| [344](https://leetcode.com/problems/reverse-string/description/) | [Reverse String](/easy/344_reverse_string) | easy | _`Two Pointers`_ | |
6062

6163
## Workflow 🌊
6264

@@ -73,8 +75,6 @@ go clean -testcache
7375
- Update `README.md` with current stats
7476
- Automaticall commits and pushes the changes
7577

76-
77-
7878
## Statistics 📊
7979

8080
- Easy: 5 solutions
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package reverse_string
2+
3+
func reverseString(s []byte) {
4+
l := 0
5+
r := len(s) - 1
6+
7+
for l < r {
8+
temp := s[l]
9+
s[l] = s[r]
10+
s[r] = temp
11+
12+
l++
13+
r--
14+
}
15+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package reverse_string
2+
3+
import "testing"
4+
5+
func TestReverseString(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
s []byte
9+
expectedResult []byte
10+
} {
11+
{
12+
name: "Test Case 1",
13+
s: []byte{'h','e','l','l','o'},
14+
expectedResult: []byte{'o','l','l','e','h'},
15+
},
16+
{
17+
name: "Test Case 1",
18+
s: []byte{'H','a','n','n','a','h'},
19+
expectedResult: []byte{'h','a','n','n','a','H'},
20+
},
21+
22+
23+
24+
}
25+
26+
for _, test := range tests {
27+
t.Run(test.name, func(t *testing.T) {
28+
reverseString(test.s)
29+
if string(test.s) != string(test.expectedResult) {
30+
t.Errorf("reverseString(%s) = %s; want = %s", test.s, test.s, test.expectedResult)
31+
}
32+
})
33+
}
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package valid_palindrome_II
2+
3+
func validPalindrome(s string) bool {
4+
l := 0
5+
r := len(s) - 1
6+
7+
for l <= r {
8+
if s[l] != s[r] {
9+
return CheckPalindrome(s, l, r - 1) || CheckPalindrome(s,l + 1, r)
10+
}
11+
l ++
12+
r --
13+
}
14+
15+
return true
16+
}
17+
18+
// Helper
19+
func CheckPalindrome(s string, i, j int) bool {
20+
for i < j {
21+
if s[i] != s[j] {
22+
return false
23+
}
24+
i ++
25+
j --
26+
}
27+
return true
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package valid_palindrome_II
2+
3+
import "testing"
4+
5+
func TestValidPalindrome(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
s string
9+
expectedResult bool
10+
} {
11+
{
12+
name: "Test Case 1",
13+
s: "aba",
14+
expectedResult: true,
15+
},
16+
{
17+
name: "Test Case 2",
18+
s: "abca",
19+
expectedResult: true,
20+
},
21+
{
22+
name: "Test Case 3",
23+
s: "abc",
24+
expectedResult: false,
25+
},
26+
}
27+
28+
for _, test := range tests {
29+
t.Run(test.name, func(t *testing.T) {
30+
got := validPalindrome(test.s)
31+
32+
if got != test.expectedResult {
33+
t.Errorf("checkPalindrome(%s) = %t; want = %t", test.s, got, test.expectedResult)
34+
}
35+
})
36+
}
37+
}

0 commit comments

Comments
 (0)