-
Notifications
You must be signed in to change notification settings - Fork 2
feat(algorithms stacks): reverse string character array #186
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
+126
−33
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # Reverse String | ||
|
|
||
| You are given a character array, s, representing a string. Write a function that reverses the array in-place. | ||
|
|
||
| - The reversal must be done by modifying the original array directly. | ||
| - You cannot use extra memory beyond a constant amount O(1). | ||
|
|
||
| ## Constraints | ||
|
|
||
| - 1 <= `s.length` <= 1000 | ||
| - Each `s[i]` is printable ASCII character | ||
|
|
||
| ## Examples | ||
|
|
||
|  | ||
|  | ||
|  | ||
|
|
||
| ## Solution | ||
|
|
||
| As the input is given as a character array, the problem can be solved efficiently by reversing the array in place without | ||
| using extra space for another array. The key idea is that reversing means swapping the first character with the last, the | ||
| second with the second-last, and so on, until all characters are placed in the correct reversed order. | ||
|
|
||
| To achieve this, we can use the two pointer technique: one pointer starts at the beginning of the array and the other at | ||
| the end. We swap the characters at each step at these two positions, then move the pointers closer toward the center. | ||
| This continues until the pointers meet or cross, ensuring all characters are reversed. | ||
|
|
||
| The steps of the algorithm are as follows: | ||
|
|
||
| 1. Initialize two pointers: left at the start, and right at the end of the array. | ||
| 2. While left < right: | ||
| - Swap the characters at s[left] and s[right]. | ||
| - Increment left by 1 and decrement right by 1 to move the pointers inward. | ||
| 3. The process terminates once the left and right pointers meet simultaneously. | ||
|
|
||
| ### Time Complexity | ||
|
|
||
| The solution’s time complexity is O(n), where nnn is the length of the input array, s. Each character in the string is | ||
| swapped at most once as the two pointers move toward the center. As every element is visited at most once, the overall | ||
| time complexity is O(n). | ||
|
|
||
| ### Space Complexity | ||
|
|
||
| The solution’s space complexity is O(1), as the reversal is done in place using only two pointers (left and right). No | ||
| additional data structures are required, regardless of the input size.Thus, the overall space complexity remains O(1). | ||
BrianLusina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+21 KB
algorithms/stack/reverse_string/images/examples/reverse_string_example_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+20.2 KB
algorithms/stack/reverse_string/images/examples/reverse_string_example_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+27.8 KB
algorithms/stack/reverse_string/images/examples/reverse_string_example_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import unittest | ||
| from typing import List | ||
| import copy | ||
| from parameterized import parameterized | ||
| from algorithms.stack.reverse_string import reverse_string, reverse_string_char_array | ||
|
|
||
| REVERSE_STRING_CHAR_ARRAY_TEST_CASES = [ | ||
| (["a", "e", "i", "o", "u"], ["u", "o", "i", "e", "a"]), | ||
| (["A", "l", "e", "x"], ["x", "e", "l", "A"]), | ||
| (["p", "y", "t", "h", "o", "n"], ["n", "o", "h", "t", "y", "p"]), | ||
| (["x"], ["x"]), | ||
| ] | ||
|
|
||
|
|
||
| class ReverseStringTestCase(unittest.TestCase): | ||
| def test_one(self): | ||
| text = "!evitacudE ot emocleW" | ||
| actual = reverse_string(text) | ||
| expected = "Welcome to Educative!" | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| @parameterized.expand(REVERSE_STRING_CHAR_ARRAY_TEST_CASES) | ||
| def test_reverse_string_char_array(self, s: List[str], expected: List[str]): | ||
| s_copy = copy.deepcopy(s) | ||
| reverse_string_char_array(s_copy) | ||
| self.assertEqual(expected, s_copy) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.