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
  • 相关阅读:
    Linux vi/vim
    Linux 磁盘管理
    Linux 用户和用户组管理
    Linux 文件与目录管理
    Linux 文件基本属性
    Linux 远程登录
    Linux 忘记密码解决方法
    Linux 系统目录结构
    Linux 系统启动过程
    linux -- su和sudo命令的区别
  • 原文地址:https://www.cnblogs.com/yawenw/p/12343498.html
Copyright © 2011-2022 走看看