Skip to content

Instantly share code, notes, and snippets.

@mathcodes
Created June 27, 2023 23:13
Show Gist options
  • Save mathcodes/9d1386c1d3b3442468fca88239fd78c2 to your computer and use it in GitHub Desktop.
Save mathcodes/9d1386c1d3b3442468fca88239fd78c2 to your computer and use it in GitHub Desktop.

Reversing a string in 4 languages: JavaScript, Java, C++, and Python

JavaScript

function reverse(str) {
  // Convert the string into an array of characters
  var arr = str.split('');

  // Reverse the array
  arr = arr.reverse();

  // Join the reversed array back into a string
  var reversedStr = arr.join('');

  // Return the reversed string
  return reversedStr;
}

In this code, the split() function is used to convert the input string into an array of characters. Then, the reverse() function is applied to reverse the order of elements in the array. Finally, the join() function is used to join the reversed array back into a string. The resulting reversed string is returned as the output.

Space complexity: O(n), where n is the length of the input string. This is because we create an array of size n to store the characters of the input string. Time complexity: The time complexity of this solution is O(n), where n is the length of the input string. This is because we iterate over half of the string to perform the swap operation.

Java

public class ReverseString {
    public static String reverse(String str) {
        char[] arr = str.toCharArray();
        int left = 0;
        int right = arr.length - 1;

        while (left < right) {
            char temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
            left++;
            right--;
        }

        return new String(arr);
    }

    public static void main(String[] args) {
        String input = "Hello, World!";
        String reversed = reverse(input);
        System.out.println(reversed);
    }
}

Time Complexity: The time complexity of this solution is O(n), where n is the length of the input string. This is because we iterate over half of the string to perform the swap operation.

Space Complexity: The space complexity is O(n) as well because we create a character array of size n to store the reversed string.

C++

#include <iostream>
#include <string>
using namespace std;

string reverse(string str) {
    int left = 0;
    int right = str.length() - 1;

    while (left < right) {
        swap(str[left], str[right]);
        left++;
        right--;
    }

    return str;
}

int main() {
    string input = "Hello, World!";
    string reversed = reverse(input);
    cout << reversed << endl;

    return 0;
}

Time Complexity: The time complexity is O(n), where n is the length of the input string. The swap operation takes constant time, and we iterate over half of the string.

Space Complexity: The space complexity is O(n) because we use a string to store the reversed string, which requires additional memory equal to the length of the input string.

Python

def reverse_string(s):
    return s[::-1]

input_string = "Hello, World!"
reversed_string = reverse_string(input_string)
print(reversed_string)

Time Complexity: The time complexity is O(n), where n is the length of the input string. The slicing operation [::-1] creates a reversed copy of the string in linear time.

Space Complexity: The space complexity is O(n) as well because the reversed string is stored in a separate copy, which requires additional memory equal to the length of the input string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment