Skip to content
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

344. Add Reverse String Problem #14

Merged
merged 2 commits into from
Jul 4, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,24 @@ public static boolean isSubsequence(String s, String t) {

return i == s.length();
}

/**
* Reverses the input array of characters in-place.
*
* @param s the array of characters to be reversed
* @implNote This method runs in {@code O(n)} time complexity and {@code O(1)} space complexity,
* where {@code n} is the length of {@code s}.
* @see <a href="https://leetcode.com/problems/reverse-string/">344. Reverse String</a>
*/
public static void reverseString(char[] s) {
int i = 0, j = s.length - 1;
while (i < j) {
var temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static com.xtenzq.arrays.TwoPointers.canBeSummed;
import static com.xtenzq.arrays.TwoPointers.isPalindrome;
import static com.xtenzq.arrays.TwoPointers.mergeSortedArrays;
import static com.xtenzq.arrays.TwoPointers.reverseString;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -281,4 +282,52 @@ void testInterleavedNonSubsequence() {
String t = "abcde";
assertFalse(isSubsequence(s, t));
}

@Test
void testReverseStringWithEvenNumberOfCharacters() {
char[] input = {'h', 'e', 'l', 'l', 'o'};
char[] expected = {'o', 'l', 'l', 'e', 'h'};
reverseString(input);
assertArrayEquals(expected, input);
}

@Test
void testReverseStringWithOddNumberOfCharacters() {
char[] input = {'a', 'b', 'c', 'd', 'e'};
char[] expected = {'e', 'd', 'c', 'b', 'a'};
reverseString(input);
assertArrayEquals(expected, input);
}

@Test
void testReverseStringWithSingleCharacter() {
char[] input = {'a'};
char[] expected = {'a'};
reverseString(input);
assertArrayEquals(expected, input);
}

@Test
void testReverseStringWithEmptyArray() {
char[] input = {};
char[] expected = {};
reverseString(input);
assertArrayEquals(expected, input);
}

@Test
void testReverseStringWithRepeatedCharacters() {
char[] input = {'a', 'a', 'a', 'a'};
char[] expected = {'a', 'a', 'a', 'a'};
reverseString(input);
assertArrayEquals(expected, input);
}

@Test
void testReverseStringWithSpecialCharacters() {
char[] input = {'!', '@', '#', '$', '%'};
char[] expected = {'%', '$', '#', '@', '!'};
reverseString(input);
assertArrayEquals(expected, input);
}
}
Loading