Strings - Question 4 Typed Out Strings (Easy)1

Strings - Question 4 Typed Out Strings (Easy)1

Solution 3

  • Utilize the original strings
  • Use two pointer technique
  • Start from the end of the strings
How I solved
  • Start from the end of the strings
  • If the letter is hash, then count the number of hash and move the pointer
  • If next letter is character, then decrease the number of hash and move the pointer
  • If both characters are not hash and there are no number of hash left on both side, then compare the character and see if they are same or not
function removeLetterSharp (s, t){
  let p = s.length - 1;
  let q = t.length - 1;
  let sHash = 0;
  let tHash = 0;
  while(p >=0 || q>=0){
    if(s[p] === "#"){
      sHash++;
      p--;
    }else{
      if(sHash > 0){
        sHash--;
        p--;
      }
    }

    if(t[q] === "#"){
      tHash++;
      q--;
    }else{
      if(tHash > 0){
        tHash--;
        q--;
      }
    }

    if(s[p] !== "#" && t[q] !== "#" && tHash === 0 && sHash === 0){
      if(s[p] !== t[q] ){
        return false;
      }
      p--;
      q--;
    }
  }
    return true;
}

console.log(removeLetterSharp("ab#c", "#a####a##c")); //true
console.log(removeLetterSharp("ab#z", "az#z")); //true
console.log(removeLetterSharp("abc#d", "acc#c")); //false
console.log(removeLetterSharp("x#y#z#", "a#")); //true
console.log(removeLetterSharp("a###b", "b")); //true
console.log(removeLetterSharp("Ab#z", "ab#z")); //false

Analyze Space and Time Complexity

  • Solution 3
    • Time Complexity : O(A+B)
    • Space Complexity : O(1)
    • Best Solution!

Class References

Class Report

2025-01-22 - Strings - Question 4 Typed Out Strings (Easy)1 Report
2025-01-22 - Developing Daily Report
2025-01-4th - Developing Weekly Report