Leetcode # 844. Backspace String Compare

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

Solution

Time Complexity: O(max(len(S), len(T)))
Space Complexity: O(1)
(The input and output generally do not count towards the space complexity.)

class Solution(object):
  def backspaceCompare(self, S, T):
    def F(S):
      skip = 0
      for i in range(len(S) - 1, -1, -1):
        x = S[i]
        if x == '#':
          skip += 1
        elif skip:
          skip -= 1
        else:
          yield x

    return all(x == y for x, y in itertools.izip_longest(F(S), F(T)))

 

Last Updated on 2023/08/16 by A1go

目錄

目錄
Bitnami