Home

344. Reverse String

Detailed explanation coming soon!

class Solution {
  public String reverseString(String s) {
    if(s == null || s.length() < 2) return s;

    char[] temp = s.toCharArray();

    for(int left = 0, right = temp.length - 1; left < right; left++, right--) {
      swap(temp, left, right);
    }

    return new String(temp);
  }

  public static void swap(char[] c, int left, int right) {
    char temp = c[left];
    c[left] = c[right];
    c[right] = temp;
  }
}


Questions? Have a neat solution? Comment below!