Solutions
LeetCode
LeetCode
  • Introduction
  • Problems
    • Algorithms
      • Easy
        • 844. Backspace String Compare
        • 905. Sort Array By Parity
    • Database
      • Easy
        • #175 Combine Two Tables
        • #181 Employees Earning More Than Their Managers
        • #182 Duplicate Emails
        • #183 Customers Who Never Order
        • #196 Delete Duplicate Emails
        • #197 Rising Temperature
        • #511 Game Play Analysis I
        • #584 Find Customer Referee
        • #586 Customer Placing the Largest Number of Orders
        • #595 Big Countries
        • #596 Classes More Than 5 Students
        • #607 Sales Person
        • #620 Not Boring Movies
        • #627 Swap Salary
        • #1050 Actors and Directors Who Cooperated At Least Three Times
        • #1084 Sales Analysis III
        • #1141 User Activity for the Past 30 Days I
        • #1148 Article Views I
        • #1179 Reformat Department Table
        • #1407 Top Travellers
        • #1484 Group Sold Products By The Date
        • #1527 Patients With a Condition
        • #1581 Customer Who Visited but Did Not Make Any Transactions
        • #1587 Bank Account Summary II
        • #1667 Fix Names in a Table
        • #1693 Daily Leads and Partners
        • #1729 Find Followers Count
        • #1741 Find Total Time Spent by Each Employee
        • #1757 Recyclable and Low Fat Products
        • #1795 Rearrange Products Table
        • #1873 Calculate Special Bonus
        • #1890 The Latest Login in 2020
        • #1965 Employees With Missing Information
      • Medium
        • #176 Second Highest Salary
        • #177 Nth Highest Salary
    • Shell
      • Easy
        • #193 Valid Phone Numbers
        • #195 Tenth Line
Powered by GitBook
On this page

Was this helpful?

  1. Problems
  2. Algorithms
  3. Easy

844. Backspace String Compare

https://leetcode.com/problems/backspace-string-compare/

public class Solution {
    public bool BackspaceCompare(string s, string t) {
        return GetString(s) == GetString(t);
    }
    
    public string GetString(string str) {
        int count = 0;
        var sb = new StringBuilder();
        for(int i = str.Length - 1; i >= 0; i--){
            if(str[i] == '#'){
                count++;
                continue;
            }
            else if(count > 0)
                count--;
            else{
                sb.Append(str[i]);
            }
        }
        return sb.ToString();
    }
}
  • Stack

public class Solution {
    public bool BackspaceCompare(string s, string t) {
        var sStack = new Stack<char>();
        var tStack = new Stack<char>();
        
        foreach(var c in s){
            if(c == '#') sStack.TryPop(out char ch);
            else sStack.Push(c);
        }
        foreach(var c in t){
            if(c == '#') tStack.TryPop(out char ch);
            else tStack.Push(c);
        }
        
        while(sStack.Count > 0 && tStack.Count > 0){
            if(sStack.Pop() != tStack.Pop()) return false;
        }
        
        return sStack.Count == 0 && tStack.Count == 0;
    }
}
PreviousEasyNext905. Sort Array By Parity

Last updated 3 years ago

Was this helpful?