DEV Community

Cover image for Day 11 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#844. Backspace String Compare(Easy/JavaScript)
KillingLeetCode
KillingLeetCode

Posted on

Day 11 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#844. Backspace String Compare(Easy/JavaScript)

Intro: I am a former accountant turned software engineer graduated from coding bootcamp in January 2022. Algorithms and Data Structure is an unavoidable part of interviews for most of the tech companies now. And one of my friends told me that you need to solve a medium leetcode problem under 60 seconds in order to get into the top tech companies.So I thought I'd start learning how to do it while job searching.

Since I have no clue on how to solve any of the problems (even the easy ones), I thought there is no point for me to waste hours and can't get it figured out. Here is my approach:

  • Pick a leetcode problem randomly or Online Assessment from targeted companies.
  • Study 1-2 solutions from Youtube or LeetCode discussion section. One brute force solution, another one more optimal.
  • Write a blog post with detailed explanation and do a verbal walk through to help understand the solutions better.
  • Code out the solution in LeetCode without looking at the solutions
  • Combat the forgetting curve: Re-do the question for the next three days. And come back regularly to revisit the problem.

844# Backspace String Compare
Difficulty: Easy Language: JavaScript

Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character.

Note that after backspacing an empty text, the text will continue empty.

Example 1:

Input: s = "ab#c", t = "ad#c"
Output: true
Explanation: Both s and t become "ac".
Enter fullscreen mode Exit fullscreen mode

Example 2:

Input: s = "ab##", t = "c#d#"
Output: true
Explanation: Both s and t become "".
Enter fullscreen mode Exit fullscreen mode

Example 3:

Input: s = "a#c", t = "b"
Output: false
Explanation: s becomes "c" while t becomes "b".
Enter fullscreen mode Exit fullscreen mode

Constraints:

  • 1 <= s.length, t.length <= 200
  • s and t only contain lowercase letters and '#' characters.

Follow up: Can you solve it in O(n) time and O(1) space?


Solution:

var backspaceCompare = function(s, t) {

    let arrayS = s.split('')
    let arrayT = t.split('')

//convert strings to arrays (note 4). This will turn "ab#c" into
//['a','b','#','c']

    let newS = []
    let newT = []

//create empty arrays to store letters from edited s and t

    for (let i = 0; i < arrayS.length; i++){

//Loop (note 1) through 'arrayS'

        if(arrayS[i] !== "#"){

//if the character is not '#'

           newS.push(arrayS[i])

//save it in the 'newS' array (note 2)

           } else newS.pop()

//if the character is '#', remove a character from array
//'newS'(note 3). For example, for array ['a','b','#','c'], 'a'
//and 'b' will be pushed to 'newS' when it gets to "#", 'b'is
//popped out from 'newS'. In the end, we will get 'newS' as
//['a','c'].

    }

    for (let j = 0; j < arrayT.length; j++){
        if(arrayT[j] !== "#"){
           newT.push(arrayT[j])
           } else newT.pop()
    }

//same concept as loop above

    if (newS.toString() === newT.toString()) {
        return true
        }

//convert array 'newS' and 'newT' to string (note 6) and return
//true if they are the same.

    return false
};
Enter fullscreen mode Exit fullscreen mode

Solution Submission detail as of 2/23/2022
(Data below could vary since there are new tests/submissions daily)

  • Runtime: 91ms
  • Memory Usage: 42.7mb

References:
LeetCode Problem Link
Note 1: Loop and Iteration
Note 2: Array.push()
Note 3: Array.pop()
Note 4: String.split()
Note 5: toString()
Blog Cover Image Credit

Top comments (0)