Skip to content
This repository was archived by the owner on Aug 14, 2024. It is now read-only.

Files

Latest commit

 

History

History
32 lines (24 loc) · 724 Bytes

344.md

File metadata and controls

32 lines (24 loc) · 724 Bytes

344. Reverse String

Description

Write a function that takes a string as input and returns the string reversed

Thinking Process

  1. Use two pointers, swap the corresponding characters

Code

public class Solution {
    public String reverseString(String s) {
        char[] word = s.toCharArray();
        int low = 0;
        int high = word.length - 1;
        while(low < high){
            char temp = word[low];
            word[low++] = word[high];
            word[high--] = temp;
        }
        return new String(word);
    }
}

Complexity

  1. Time complexity is O(n) as the string is scanned once
  2. Space complexity is O(n) as there's no way to maniplate strings in place in Java