zoukankan      html  css  js  c++  java
  • leetcode844 Backspace String Compare

     1 """
     2 Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.
     3 Example 1:
     4 Input: S = "ab#c", T = "ad#c"
     5 Output: true
     6 Explanation: Both S and T become "ac".
     7 Example 2:
     8 Input: S = "ab##", T = "c#d#"
     9 Output: true
    10 Explanation: Both S and T become "".
    11 Example 3:
    12 Input: S = "a##c", T = "#a#c"
    13 Output: true
    14 Explanation: Both S and T become "c".
    15 Example 4:
    16 Input: S = "a#c", T = "b"
    17 Output: false
    18 Explanation: S becomes "c" while T becomes "b".
    19 """
    20 """
    21 简单题通过
    22 """
    23 class Solution:
    24     def backspaceCompare(self, S, T):
    25         """
    26         :type S: str
    27         :type T: str
    28         :rtype: bool
    29         """
    30         return self._back(S) == self._back(T)
    31     def _back(self, string):
    32         stack = []
    33         for i in range(len(string)):
    34             if string[i] == '#':
    35                 if stack: #这里重要,需要判断stack不为空,对于这种[#a#c]输入
    36                     stack.pop()
    37                 continue
    38             stack.append(string[i])
    39         return stack
  • 相关阅读:
    Path 环境变量
    Scala_ 类_可见性
    ubuntu16.04 注意特别出
    Python基础之文件操作
    python之set集合操作
    python数据类型之字典操作
    python的数据类型之列表list
    Python的条件控制及循环
    使用jmeter做接口测试
    AMD64 专业名词缩写
  • 原文地址:https://www.cnblogs.com/yawenw/p/12343498.html
Copyright © 2011-2022 走看看