|
| 1 | +/** |
| 2 | + * 0344. Reverse String |
| 3 | + * |
| 4 | + * Difficulty: easy |
| 5 | + * Tags: two-pointers, string |
| 6 | + * |
| 7 | + * Description: |
| 8 | + * Write a function that reverses a string. The input string is given as an array of characters `s`. |
| 9 | + * |
| 10 | + * You must do this by modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm) with `O(1)` extra memory. |
| 11 | + * |
| 12 | + * Examples: |
| 13 | + * 1. Input: s = \["h","e","l","l","o"\] |
| 14 | + * Output: \["o","l","l","e","h"\] |
| 15 | + * 2. Input: s = \["H","a","n","n","a","h"\] |
| 16 | + * Output: \["h","a","n","n","a","H"\] |
| 17 | + * |
| 18 | + * Constraints: |
| 19 | + * - `1 <= s.length <= 10^5` |
| 20 | + * - `s[i]` is a [printable ascii character](https://en.wikipedia.org/wiki/ASCII#Printable_characters). |
| 21 | + * |
| 22 | + */ |
| 23 | +import { z } from 'zod'; |
| 24 | +import type { TestCase } from '../packages/src/types.js'; |
| 25 | + |
| 26 | +export const SolutionSchema = z.function({ |
| 27 | + input: [z.array(z.string().length(1))], |
| 28 | + output: z.array(z.string().length(1)), |
| 29 | +}); |
| 30 | + |
| 31 | +export type Solution = z.infer<typeof SolutionSchema>; |
| 32 | + |
| 33 | +export const cases: TestCase<Solution>[] = [ |
| 34 | + { |
| 35 | + input: [['h', 'e', 'l', 'l', 'o']], |
| 36 | + expected: ['o', 'l', 'l', 'e', 'h'], |
| 37 | + name: 'Example 1', |
| 38 | + }, |
| 39 | + { |
| 40 | + input: [['H', 'a', 'n', 'n', 'a', 'h']], |
| 41 | + expected: ['h', 'a', 'n', 'n', 'a', 'H'], |
| 42 | + name: 'Example 2', |
| 43 | + }, |
| 44 | +]; |
| 45 | + |
| 46 | +/** |
| 47 | + * Recursive Solution |
| 48 | + * Approach: |
| 49 | + * - Use a recursive function to reverse the array |
| 50 | + * - Return the reversed array |
| 51 | + * Time Complexity: O(n) |
| 52 | + * Space Complexity: O(n) |
| 53 | + */ |
| 54 | +export const recursiveSolution = SolutionSchema.implement((s) => { |
| 55 | + if (s.length <= 1) { |
| 56 | + return s; |
| 57 | + } |
| 58 | + const helper = (left: number, right: number) => { |
| 59 | + if (left >= right) { |
| 60 | + return; |
| 61 | + } |
| 62 | + [s[left], s[right]] = [s[right], s[left]]; |
| 63 | + helper(left + 1, right - 1); |
| 64 | + }; |
| 65 | + helper(0, s.length - 1); |
| 66 | + return s; |
| 67 | +}); |
| 68 | + |
| 69 | +/** |
| 70 | + * Iterative Solution |
| 71 | + * Approach: |
| 72 | + * - Use a while loop to reverse the array |
| 73 | + * - Return the reversed array |
| 74 | + * Time Complexity: O(n) |
| 75 | + * Space Complexity: O(1) |
| 76 | + */ |
| 77 | +export const iterativeSolution = SolutionSchema.implement((s) => { |
| 78 | + if (s.length <= 1) { |
| 79 | + return s; |
| 80 | + } |
| 81 | + let left = 0; |
| 82 | + let right = s.length - 1; |
| 83 | + let temp: string; |
| 84 | + while (left < right) { |
| 85 | + temp = s[left]; |
| 86 | + s[left] = s[right]; |
| 87 | + s[right] = temp; |
| 88 | + left++; |
| 89 | + right--; |
| 90 | + } |
| 91 | + return s; |
| 92 | +}); |
| 93 | + |
| 94 | +export const solutions = [recursiveSolution, iterativeSolution]; |
0 commit comments