Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.

Example 1:

Input: S = "ab#c", T = "ad#c"
Output: true
Explanation: Both S and T become "ac".

Example 2:

Input: S = "ab##", T = "c#d#"
Output: true
Explanation: Both S and T become "".

Example 3:

Input: S = "a##c", T = "#a#c"
Output: true
Explanation: Both S and T become "c".

Example 4:

Input: S = "a#c", T = "b"
Output: false
Explanation: S becomes "c" while T becomes "b".

Note:

  1. 1 <= S.length <= 200
  2. 1 <= T.length <= 200
  3. S and T only contain lowercase letters and '#' characters.

Follow up:

  • Can you solve it in O(N) time and O(1) space?

建一个helper function, 然后用stack, 如果为'#', 就stack.pop(), 否则append(c), 最后返回"".join(stack).  T: O(m+n)    S: O(m+n)

**imporve, 可以用two pointers 去实现 T: O(m+n)   S: O(1)

Code

class Solution:
def backspaceStringCompare(self, S, T):
def helper(s):
stack = []
for c in s:
if c == '#' and stack:
stack.pop()
elif c != '#':
stack.append(c)
return "".join(stack)
return helper(S) == helper(T)

最新文章

  1. 用JWT来保护我们的ASP.NET Core Web API
  2. 浅谈cssText
  3. ORACLE数据库的导入及导出
  4. HTML 5 代码
  5. NOIP 2015 游记
  6. PAT 1027. 打印沙漏(20)
  7. Category和Extension
  8. android平台手电筒开发源代码
  9. Spring框架学习之第5节
  10. ng-cookie 的基本使用
  11. .Net学习难点讨论系列17 - 线程本地变量的使用
  12. Laravel Migrate
  13. 宝爷Debug小记——Cocos2d-x(3.13之前的版本)底层BUG导致Spine渲染花屏
  14. win10 uwp 切换主题
  15. Android 根据字符串动态获取资源ID
  16. C语言头文件引用
  17. iview服务不可以被访问解决办法
  18. flask基本介绍及虚拟环境
  19. java_线程
  20. php调试用的几个小方法

热门文章

  1. 【分享】IT产业中的三大定理(三) —— 反摩尔定理 (Reverse Moore&#39;s Law)
  2. mac Intellij Idea Tmocat 启动报 Error running Tomcat: /conf/Catalina
  3. 第一步 使用sencha touch cmd 4.0 创建项目、打包(加入全局变量、公用类、自定义扩展、资源文件)
  4. Google浏览器清除缓存快捷键
  5. 记录一下gitlab通过CAS登录慢的问题
  6. Centos 升级 python
  7. javaAgent 参数
  8. 【CF896D】Nephren Runs a Cinema 卡特兰数+组合数+CRT
  9. Unity3D笔记 愤怒的小鸟&lt;七&gt; 小鸟群准备动画
  10. Function学习