diff --git a/README.md b/README.md
index 9e4351b..e831a33 100644
--- a/README.md
+++ b/README.md
@@ -34,6 +34,7 @@ continually updating π.
* [67. Add Binary](./src/0067_add_binary/add_binary.go) *`brute force`*
* [76. Minimum Window Substring](./src/0076_minimum_window_substring/minimum_window_substring.go) *`sliding window`*
* [125. Valid Palindrome](./src/0125_valid_palindrome/valid_palindrome.go) *`string;`* *`double index`*
+* [344. Reverse String](0344_reverse_string/reverse_string.go) *`string;`* *`double index`*
* [438. Find All Anagrams in a String](./src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go) *`sliding window`*
### Linked List
diff --git a/src/0344_reverse_string/reverse_string.go b/src/0344_reverse_string/reverse_string.go
new file mode 100644
index 0000000..520cf18
--- /dev/null
+++ b/src/0344_reverse_string/reverse_string.go
@@ -0,0 +1,41 @@
+/*
+344. Reverse String
+https://leetcode.com/problems/reverse-string/
+
+Write a function that takes a string as input and returns the string reversed.
+*/
+// time: 2018εΉ΄12ζ26ζ₯
+
+package reversestring
+
+// double index
+// time complexity: O(n)
+// space complexity: O(1)
+func reverseString(s string) string {
+ var (
+ l int
+ r = len(s) - 1
+ )
+
+ for r>= l {
+ charL := s[l]
+ charR := s[r]
+ s = s[:r] + string(charL) + s[r+1:]
+ s = s[:l] + string(charR) + s[l+1:]
+ r--
+ l++
+ }
+ return s
+}
+
+// double index
+// time complexity: O(n)
+// space complexity: O(n)
+func reverseString1(s string) string {
+ sLen := len(s)
+ runes := []rune(s)
+ for i := 0; i < sLen/2; i++ { + runes[i], runes[sLen-i-1] = runes[sLen-i-1], runes[i] + } + return string(runes) +} diff --git a/src/0344_reverse_string/reverse_string_test.go b/src/0344_reverse_string/reverse_string_test.go new file mode 100644 index 0000000..784c521 --- /dev/null +++ b/src/0344_reverse_string/reverse_string_test.go @@ -0,0 +1,45 @@ +package reversestring + +import "testing" + +func TestReverseString(t *testing.T) { + testCases := []string{ + "hello", + "A man, a plan, a canal: Panama", + "", + "ab", + } + expected := []string{ + "olleh", + "amanaP :lanac a ,nalp a ,nam A", + "", + "ba", + } + + for index, data := range testCases { + if res := reverseString(data); res != expected[index] { + t.Errorf("expected %s, got %s", expected[index], res) + } + } +} + +func TestReverseString1(t *testing.T) { + testCases := []string{ + "hello", + "A man, a plan, a canal: Panama", + "", + "ab", + } + expected := []string{ + "olleh", + "amanaP :lanac a ,nalp a ,nam A", + "", + "ba", + } + + for index, data := range testCases { + if res := reverseString1(data); res != expected[index] { + t.Errorf("expected %s, got %s", expected[index], res) + } + } +} diff --git a/src/README.md b/src/README.md index 003f284..c1c10f0 100644 --- a/src/README.md +++ b/src/README.md @@ -40,6 +40,7 @@ |0283|[Move Zeroes(solution1)](./0283_move_zeroes/move_zeroes.go)
[Move Zeroes(solution2)](./0283_move_zeroes/move_zeroes2.go)|Easy|*`array`*|
|0300|[Longest Increasing Subsequence](./0300_longest_increasing_subsequence/lis.go)|Medium|*`dp`*|
|0343|[Integer Break](./0343_integer_break/integer_break.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
+|0344|[344. Reverse String](0344_reverse_string/reverse_string.go)|Easy|*`double index`*|
|0349|[Intersection of Two Arrays](./0349_intersection_of_2_arrays/intersection_of_two_arrays.go)|Easy|*`set`*|
|0350| [Intersection of Two Arrays II](./0350_intersection_of_two_arrays2/intersection_of_two_arrays2.go)|Easy|*`map`*|
|0376|[Wiggle Subsequence](./0376_wiggle_subsequence/wiggle_subsequence.go)|Medium|*`dp`*|