Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LintCode: Input Stream

Posted on February 24, 2018July 26, 2020 by braindenny

Input Stream



Similar Problems:

  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #stack

Give two input stream inputA and inputB, which have Backspace. If the final result of the two input streams is equal, output YES, otherwise output NO.

Notice

  • Input characters include only lowercase letters and ‘<‘
  • The length of input stream does not exceed 10000.

Example

Given inputA = "abcde<<", inputB = "abcd<e<", return "YES".

Explanation:
The final result of inputA and inputB is abc, so return "YES".
Given inputA = "a<<bc", inputB = "abc<", return "NO"

Explanation:
The final result of inputA is "bc", and the final result of inputB is "ab", so return "NO".

Github: code.dennyzhang.com

Credits To: LintCode.com

Leave me comments, if you have better ways to solve.


## https://code.dennyzhang.com/input-stream
#!/usr/bin/env python
class Solution:
    """
    @param inputA: Input stream A
    @param inputB: Input stream B
    @return: The answer
    """
    def inputStream(self, inputA, inputB):
       # Basic Ideas: stack
        stack1, stack2 = [], []
        for ch in inputA:
            if ch == '<':
                if len(stack1) != 0: stack1.pop(-1)
            else:
                stack1.append(ch)
        for ch in inputB:
            if ch == '<':
               if len(stack2) != 0:
                   stack2.pop(-1)
            else:
                stack2.append(ch)
        return 'YES' if stack1 == stack2 else 'NO'
linkedin
github
slack

Post Views: 8
Posted in BasicTagged #stack

Post navigation

LintCode: Single Number IV
LintCode: Computer Maintenance

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.