Skip to content

Leetcode 680 + 344 #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ go clean -testcache
| [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 |
| [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 |
| [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 |
| [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 |
| [344](https://leetcode.com/problems/reverse-string/description/) | [Reverse String](/easy/344_reverse_string) | easy | _`Two Pointers`_ | |

## Workflow 🌊

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



## Statistics 📊

- Easy: 5 solutions
Expand Down
15 changes: 15 additions & 0 deletions easy/344_reverse_string/344_reverse_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package reverse_string

func reverseString(s []byte) {
l := 0
r := len(s) - 1

for l < r {
temp := s[l]
s[l] = s[r]
s[r] = temp

l++
r--
}
}
34 changes: 34 additions & 0 deletions easy/344_reverse_string/344_reverse_string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package reverse_string

import "testing"

func TestReverseString(t *testing.T) {
tests := []struct {
name string
s []byte
expectedResult []byte
} {
{
name: "Test Case 1",
s: []byte{'h','e','l','l','o'},
expectedResult: []byte{'o','l','l','e','h'},
},
{
name: "Test Case 1",
s: []byte{'H','a','n','n','a','h'},
expectedResult: []byte{'h','a','n','n','a','H'},
},



}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
reverseString(test.s)
if string(test.s) != string(test.expectedResult) {
t.Errorf("reverseString(%s) = %s; want = %s", test.s, test.s, test.expectedResult)
}
})
}
}
28 changes: 28 additions & 0 deletions easy/680_valid_palindrome_II/680_valid_palindrome_II.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package valid_palindrome_II

func validPalindrome(s string) bool {
l := 0
r := len(s) - 1

for l <= r {
if s[l] != s[r] {
return CheckPalindrome(s, l, r - 1) || CheckPalindrome(s,l + 1, r)
}
l ++
r --
}

return true
}

// Helper
func CheckPalindrome(s string, i, j int) bool {
for i < j {
if s[i] != s[j] {
return false
}
i ++
j --
}
return true
}
37 changes: 37 additions & 0 deletions easy/680_valid_palindrome_II/680_valid_palindrome_II_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package valid_palindrome_II

import "testing"

func TestValidPalindrome(t *testing.T) {
tests := []struct {
name string
s string
expectedResult bool
} {
{
name: "Test Case 1",
s: "aba",
expectedResult: true,
},
{
name: "Test Case 2",
s: "abca",
expectedResult: true,
},
{
name: "Test Case 3",
s: "abc",
expectedResult: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := validPalindrome(test.s)

if got != test.expectedResult {
t.Errorf("checkPalindrome(%s) = %t; want = %t", test.s, got, test.expectedResult)
}
})
}
}