Asked in Samsung


#1

Given a string, your task is to find the number of patterns of form 1[0]1 where [0] represents any number of zeroes (minimum requirement is one 0) there should not be any other character except 0 in the [0] sequence.


#2

Pseduo Code in python

def pattern_count(string):
    flag_index = string.find('1') # get the first '1' in string
    count = 0 # count number of patterns
    zero_between = 0 # count number of zero between two 1
    i = flag_index # start at first '1' in string
    while i < len(string) and i != -1:
        if string[i] == '0':
            zero_between += 1
        elif string[i] == '1':
            if zero_between > 0:
                count += 1
                zero_between = 0
            flag_index = i
        else:
            zero_between = 0
            flag_index = string.find('1', flag_index+1)
            i = flag_index
        i += 0 if i == -1 else 1
    print count`Preformatted text`