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 19, 2019
1 parent 6d04d71 commit 1402cc6
Show file tree
Hide file tree
Showing 4 changed files with 54 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
}

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


/**
# 844. Backspace String Compare

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

- SeeAlso:
[Link](https://leetcode.com/problems/third-maximum-number/)

- 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 1402cc6

Please sign in to comment.