Home leetCode Day12
Post
Cancel

leetCode Day12

Backspace String Compare

題目:

1
給兩個字串,如果兩個陣列相等回傳true,'#'為消除前一個字串

解法:

code

func backspaceCompare(s string, t string) bool {
    x := len(s) - 1
    y := len(t) - 1
    
    count := 0
    for true {
        for x >= 0 && (s[x] == 35 || count > 0) {
            if s[x] != 35 {
                count--
            } else {
                count++
            }
            x--
        }
        count = 0
        for y >=0 && (t[y] == 35 || count > 0) {
            if t[y] != 35 {
                count--
            } else {
                count++
            }
            y--
        }
        
        if x >= 0 && y >= 0 && s[x] == t[y] {
            x--
            y--
        } else {
            break
        }
    }

    return x == -1 && y == -1
}
This post is licensed under CC BY 4.0 by the author.

Trending Tags