Skip to main content
Stupidly forgot to return what's left in the stack :\
Source Link
hodisr
  • 173
  • 4

From my experience in job interviews that question usually takes you to implements a solution with one type of data-structure.

A good implementation will be using a stack that load each '(' bracket and unloads each ')' bracket. In that way you can iterate over the text and if you encounter ') bracket beforehand you add to the result.

In Python you can use list type object as a Stack, reference.

Implementation example using stack in python3:

def bracket_match(text):
    pairs = {'(': ')'}  # Using dictionary for O1 get
    sk = []
    res = 0
    for c in text:
        if c in pairs:
            sk.append(pairs[c])
        elif sk and c == sk[-1]:
            sk.pop()
        else:
            res += 1
    return res + len(sk)

From my experience in job interviews that question usually takes you to implements a solution with one type of data-structure.

A good implementation will be using a stack that load each '(' bracket and unloads each ')' bracket. In that way you can iterate over the text and if you encounter ') bracket beforehand you add to the result.

In Python you can use list type object as a Stack, reference.

Implementation example using stack in python3:

def bracket_match(text):
    pairs = {'(': ')'}  # Using dictionary for O1 get
    sk = []
    res = 0
    for c in text:
        if c in pairs:
            sk.append(pairs[c])
        elif sk and c == sk[-1]:
            sk.pop()
        else:
            res += 1
    return res

From my experience in job interviews that question usually takes you to implements a solution with one type of data-structure.

A good implementation will be using a stack that load each '(' bracket and unloads each ')' bracket. In that way you can iterate over the text and if you encounter ') bracket beforehand you add to the result.

In Python you can use list type object as a Stack, reference.

Implementation example using stack in python3:

def bracket_match(text):
    pairs = {'(': ')'}  # Using dictionary for O1 get
    sk = []
    res = 0
    for c in text:
        if c in pairs:
            sk.append(pairs[c])
        elif sk and c == sk[-1]:
            sk.pop()
        else:
            res += 1
    return res + len(sk)
added 1 character in body
Source Link
hodisr
  • 173
  • 4

From my experience in job interviews that question usually takes you to implements a solution with one type of data-structure.

A good implementation will be using a stack that load each '(' bracket and unloads each ')' bracket. In that way you can iterate over the text and if you encounter ') bracket beforehand you add to the result.

In Python you can use list type object as a Stack,  reference.

Implementation example using stack in python3:

def bracket_match(text):
    pairs = {'(': ')'}  # Using dictionary for O1 get
    sk = []
    res = 0
    for c in text:
        if c in pairs:
            sk.append(pairs[c])
        elif sk and c == sk[-1]:
            sk.pop()
        else:
            res += 1
    return res

From my experience in job interviews that question usually takes you to implements a solution with one type of data-structure.

A good implementation will be using a stack that load each '(' bracket and unloads each ')' bracket. In that way you can iterate over the text and if you encounter ') bracket beforehand you add to the result.

In Python you can use list type object as a Stack,reference.

Implementation example using stack in python3:

def bracket_match(text):
    pairs = {'(': ')'}  # Using dictionary for O1 get
    sk = []
    res = 0
    for c in text:
        if c in pairs:
            sk.append(pairs[c])
        elif sk and c == sk[-1]:
            sk.pop()
        else:
            res += 1
    return res

From my experience in job interviews that question usually takes you to implements a solution with one type of data-structure.

A good implementation will be using a stack that load each '(' bracket and unloads each ')' bracket. In that way you can iterate over the text and if you encounter ') bracket beforehand you add to the result.

In Python you can use list type object as a Stack,  reference.

Implementation example using stack in python3:

def bracket_match(text):
    pairs = {'(': ')'}  # Using dictionary for O1 get
    sk = []
    res = 0
    for c in text:
        if c in pairs:
            sk.append(pairs[c])
        elif sk and c == sk[-1]:
            sk.pop()
        else:
            res += 1
    return res
Source Link
hodisr
  • 173
  • 4

From my experience in job interviews that question usually takes you to implements a solution with one type of data-structure.

A good implementation will be using a stack that load each '(' bracket and unloads each ')' bracket. In that way you can iterate over the text and if you encounter ') bracket beforehand you add to the result.

In Python you can use list type object as a Stack,reference.

Implementation example using stack in python3:

def bracket_match(text):
    pairs = {'(': ')'}  # Using dictionary for O1 get
    sk = []
    res = 0
    for c in text:
        if c in pairs:
            sk.append(pairs[c])
        elif sk and c == sk[-1]:
            sk.pop()
        else:
            res += 1
    return res