Skip to content

Commit

Permalink
[844_e] Backspace String Compare
Browse files Browse the repository at this point in the history
  • Loading branch information
Wei Cao authored and Wei Cao committed Feb 20, 2019
1 parent d02457e commit 8eed803
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
19 changes: 19 additions & 0 deletions TSWeiFramework/Solution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,23 @@ public extension Solution{
}
return (r.2 == Int.min) ? r.0 : r.2
}

/**
844, Backspace String Compare
*/
func backspaceCompare(_ S: String, _ T: String) -> Bool {
return removeBackspace(S) == removeBackspace(T)
}
private func removeBackspace(_ s: String) -> String{
return s.reduce(into: "") { (r, c) in
switch c {
case _ where c != "#":
r.append(c)
case "#" where r.count > 0:
_ = r.removeLast()
default:
break
}
}
}
}
5 changes: 5 additions & 0 deletions TesoraShot/Solutions/Ray/SolutionsRay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ class SolutionsRay: SolutionsProtocol {
func thirdMax(_ nums: [Int]) -> Int {
return -1
}

func backspaceCompare(_ S: String, _ T: String) -> Bool {
return false
}

}
29 changes: 29 additions & 0 deletions TesoraShot/Solutions/SolutionsProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,33 @@ protocol SolutionsProtocol {
- Returns: The third maximum number in this array; the first maximum number otherwise.
*/
func thirdMax(_ nums: [Int]) -> Int


/**
# 844. Backspace String Compare

Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.

For example:
```
Input: S = "ab#c", T = "ad#c"
```
A solution set is:
```
Output: true
Explanation: Both S and T become "ac".
```

- Parameters:
- S: String with "#" means a backspace character
- T: String with "#" means a backspace character

- Complexity: *O(n)* time, *O(1)* space

- SeeAlso:
[Link](https://leetcode.com/problems/backspace-string-compare/)

- Returns: they are equal.
*/
func backspaceCompare(_ S: String, _ T: String) -> Bool
}
5 changes: 5 additions & 0 deletions TesoraShot/Solutions/Wei/SolutionsWei.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation
import TSWeiFramework

class SolutionsWei: SolutionsProtocol {

let s = Solution()

func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
Expand All @@ -19,4 +20,8 @@ class SolutionsWei: SolutionsProtocol {
func thirdMax(_ nums: [Int]) -> Int {
return s.thirdMax(nums)
}

func backspaceCompare(_ S: String, _ T: String) -> Bool {
return s.backspaceCompare(S, T)
}
}

0 comments on commit 8eed803

Please sign in to comment.