DEV Community

King coder
King coder

Posted on

🧠 Day 26 of Learning DSA: Solved Two LeetCode Problems (Plus One & Reverse String)

Problem 1: Plus One

🧩 Problem Statement:

You are given a large integer represented as an array of digits, where each digits[i] is a digit of the integer. Add one to the integer and return the result as an array.

Question Link

βœ… Approach:

  • Start from the last digit.
  • If the digit is less than 9, just add 1 and return.
  • If the digit is 9, it becomes 0 (carry over).
  • If all digits are 9, insert a 1 at the beginning (e.g., [9,9,9] β†’ [1,0,0,0]).

Complexity

  • Time Complexity: O(n) β€” Might loop through all digits.
  • Space Complexity: O(1) β€” In-place, except final carry using unshift.

Example Code

/**
 * @param {number[]} digits
 * @return {number[]}
 */

var plusOne = function(digits) {
    for (let i = digits.length - 1; i >= 0; i--) {
        if (digits[i] < 9) {
            digits[i] += 1;
            return digits; 
        }
        digits[i] = 0; 
    }

    digits.unshift(1); 
    return digits;
};
Enter fullscreen mode Exit fullscreen mode

How the Code Work

The code uses a loop that starts from the last element of the digits array and moves backward.

  • For each digit, it checks if the digit is less than 9.

    • If it is, the code adds 1 to that digit and immediately returns the updated array. This means the loop stops, and no further digits are changed.
  • If the digit is 9, it is replaced with 0, and the loop continues to the previous digit to handle the carry-over.

  • If all digits are 9 (for example, [9, 9, 9]), after the loop completes, the code inserts a 1 at the beginning of the array and returns the updated array (e.g., [1, 0, 0, 0]).

This approach ensures that the addition of one is handled correctly, including any necessary carry-overs.

Problem 2: Reverse String

Question Link

🧩 Problem Statement:

Given a character array s, reverse the array in-place. Do not allocate extra space for another array β€” modify the input array directly with O(1) extra memory.


βœ… Approach:

  • First, calculate the length of the array.
  • Compute half of the length to determine how many swaps are needed.
  • Use a loop to iterate from the beginning of the array to the middle.
    • In each iteration:
    • Store the character at the current index in a temporary variable.
    • Swap the current character with its corresponding character from the end of the array.
    • Replace the character at the end with the value from the temporary variable.
  • Continue this process until the loop reaches the middle of the array.

This technique ensures that each character is swapped only once, resulting in an in-place reversal of the array.


🧠 Time and Space Complexity:

  • Time Complexity: O(n)

    Each element is visited once in the process of swapping, where n is the length of the array.

  • Space Complexity: O(1)

    The reversal is done in-place using only a temporary variable for swapping.

Example Code

/**
 * @param {character[]} s
 * @return {void} Do not return anything, modify s in-place instead.
 */

var reverseString = function(s) {
let length = s.length;
let half = Math.round(length / 2)
let temp = null;

for(let j = 0 ; j < half ; j++){
  temp = s[j];
  let charB = s[length - 1 - j]
  s[j] = charB;
  s[length - 1 - j] = temp;

}

};

reverseString(["h","e","l","l","o"])
Enter fullscreen mode Exit fullscreen mode

πŸ” How the Code Works

  • The function begins by determining the total length of the character array s.
  • It then calculates half of the length (rounded) to know how many times to loop. This is because we only need to swap characters up to the midpoint of the array.
  • A temporary variable is used to hold the value of one character during the swap.

  • Inside the loop:

    • It picks a character from the front of the array.
    • It also finds the corresponding character from the end of the array.
    • These two characters are swapped:
    • The front character is temporarily stored.
    • The character from the end is placed at the front index.
    • The temporarily stored character is placed at the end index.
  • This process continues until all characters have been swapped in-place, resulting in a reversed array.

πŸ’‘ Example:
For input ["h", "e", "l", "l", "o"]:

  • After swapping: ["o", "l", "l", "e", "h"]

Top comments (0)