countConstruct("purple", ["purp", "p", "ur", "le", "purpl"]) -> 2
m = target
n = wordBank.length
first create an array the size of the target.length + 1
set default value to be 0
index : 0 1 2 3 4 5 6
value : 0 0 0 0 0 0 0
Actual char: p u r p l e
when target value is an empty string, no string concatenation is required to get "", therefore return value should be 1
index : 0 1 2 3 4 5 6
value : 1 0 0 0 0 0 0
Actual char: p u r p l e
look at the 1st element of the array ["purp", "p", "ur", "le", "purpl"] is "purp"
current index is 0, value is 1, and actual char is "p"
since first char of "purp" === actual char "p"
we can look at "purp" char length 4 steps ahead of the current index,
value can be changed to the same as current value 1
index : 0 1 2 3 4 5 6
value : 1 0 0 0 1 0 0
Actual char: p u r p l e
look at the 2nd element of the array ["purp", "p", "ur", "le", "purpl"] is "p"
current index is 0, value is 1, and actual char is "p"
since first char of "p" === actual char "p"
we can look at "p" char length 1 steps ahead of the current index,
value can be changed to the same as current value 1
index : 0 1 2 3 4 5 6
value : 1 1 0 0 1 0 0
Actual char: p u r p l e
look at the 3rd element of the array ["purp", "p", "ur", "le", "purpl"] is "ur"
current index is 0, value is 1, and actual char is "p"
since first char of "ur" !== actual char "p"
we can ignore and skip this
index : 0 1 2 3 4 5 6
value : 1 1 0 0 1 0 0
Actual char: p u r p l e
look at the 4th element of the array ["purp", "p", "ur", "le", "purpl"] is "le"
current index is 0, value is 1, and actual char is "p"
since first char of "le" !== actual char "p"
we can ignore and skip this
index : 0 1 2 3 4 5 6
value : 1 1 0 0 1 0 0
Actual char: p u r p l e
look at the 5th element of the array ["purp", "p", "ur", "le", "purpl"] is "purpl"
current index is 0, value is 1, and actual char is "p"
since first char of "purpl" === actual char "p"
we can look at "purpl" char length 5 steps ahead of the current index,
value can be changed to the same as current value 1
index : 0 1 2 3 4 5 6
value : 1 1 0 0 1 1 0
Actual char: p u r p l e
move current value to the next index
look at the 1st element of the array ["purp", "p", "ur", "le", "purpl"] is "purp"
current index is 1, value is 1, and actual char is "u"
since first char of "purp" !== actual char "u"
we can ignore and skip this
index : 0 1 2 3 4 5 6
value : 1 1 0 0 1 1 0
Actual char: p u r p l e
look at the 2nd element of the array ["purp", "p", "ur", "le", "purpl"] is "p"
current index is 1, value is 1, and actual char is "u"
since first char of "p" !== actual char "u"
we can ignore and skip this
index : 0 1 2 3 4 5 6
value : 1 1 0 0 1 1 0
Actual char: p u r p l e
look at the 3rd element of the array ["purp", "p", "ur", "le", "purpl"] is "ur"
current index is 1, value is 1, and actual char is "u"
since first char of "ur" === actual char "u"
we can look at "ur" char length 2 steps ahead of the current index,
value can be changed to the same as current value 1
index : 0 1 2 3 4 5 6
value : 1 1 0 1 1 1 0
Actual char: p u r p l e
look at the 4th element of the array ["purp", "p", "ur", "le", "purpl"] is "le"
current index is 1, value is 1, and actual char is "u"
since first char of "le" !== actual char "u"
we can ignore and skip this
index : 0 1 2 3 4 5 6
value : 1 1 0 1 1 1 0
Actual char: p u r p l e
look at the 5th element of the array ["purp", "p", "ur", "le", "purpl"] is "purpl"
current index is 1, value is 1, and actual char is "u"
since first char of "purpl" !== actual char "u"
we can ignore and skip this
index : 0 1 2 3 4 5 6
value : 1 1 0 1 1 1 0
Actual char: p u r p l e
move current value to the next index
look at the 1st element of the array ["purp", "p", "ur", "le", "purpl"] is "purp"
current index is 2, value is 0, and actual char is "r"
since value is 0
we can ignore and skip the entire process and move to the next index
index : 0 1 2 3 4 5 6
value : 1 1 0 1 1 1 0
Actual char: p u r p l e
move current value to the next index
look at the 1st element of the array ["purp", "p", "ur", "le", "purpl"] is "purp"
current index is 3, value is 1, and actual char is "p"
since first char of "purp" === actual char "p"
we can look at "purp" char length 4 steps ahead of the current index,
it is out of range, nothing needs to be changed
index : 0 1 2 3 4 5 6
value : 1 1 0 1 1 1 0
Actual char: p u r p l e
look at the 2nd element of the array ["purp", "p", "ur", "le", "purpl"] is "p"
current index is 3, value is 1, and actual char is "p"
since first char of "p" === actual char "p"
we can look at "p" char length 1 steps ahead of the current index,
existing value of 1 can be added to the current value 1 changing the value to 2
index : 0 1 2 3 4 5 6
value : 1 1 0 1 2 1 0
Actual char: p u r p l e
look at the 3rd element of the array ["purp", "p", "ur", "le", "purpl"] is "ur"
current index is 3, value is 1, and actual char is "p"
since first char of "ur" !== actual char "p"
we can ignore and skip this
index : 0 1 2 3 4 5 6
value : 1 1 0 1 2 1 0
Actual char: p u r p l e
look at the 4th element of the array ["purp", "p", "ur", "le", "purpl"] is "le"
current index is 3, value is 1, and actual char is "p"
since first char of "le" !== actual char "p"
we can ignore and skip this
index : 0 1 2 3 4 5 6
value : 1 1 0 1 2 1 0
Actual char: p u r p l e
look at the 5th element of the array ["purp", "p", "ur", "le", "purpl"] is "purpl"
current index is 3, value is 1, and actual char is "p"
since first char of "purpl" === actual char "p"
we can look at "purpl" char length 5 steps ahead of the current index,
it is out of range, nothing needs to be changed
index : 0 1 2 3 4 5 6
value : 1 1 0 1 2 1 0
Actual char: p u r p l e
move current value to the next index
look at the 1st element of the array ["purp", "p", "ur", "le", "purpl"] is "purp"
current index is 4, value is 2, and actual char is "l"
since first char of "purp" !== actual char "l"
we can ignore and skip this
index : 0 1 2 3 4 5 6
value : 1 1 0 1 2 1 0
Actual char: p u r p l e
look at the 2nd element of the array ["purp", "p", "ur", "le", "purpl"] is "p"
current index is 4, value is 2, and actual char is "l"
since first char of "p" !== actual char "l"
we can ignore and skip this
index : 0 1 2 3 4 5 6
value : 1 1 0 1 2 1 0
Actual char: p u r p l e
look at the 3rd element of the array ["purp", "p", "ur", "le", "purpl"] is "ur"
current index is 4, value is 2, and actual char is "l"
since first char of "ur" !== actual char "l"
we can ignore and skip this
index : 0 1 2 3 4 5 6
value : 1 1 0 1 2 1 0
Actual char: p u r p l e
look at the 4th element of the array ["purp", "p", "ur", "le", "purpl"] is "le"
current index is 4, value is 2, and actual char is "l"
since first char of "le" === actual char "l"
we can look at "le" char length 2 steps ahead of the current index,
existing value of 0 can be added to the current value 2 changing the value to 2
index : 0 1 2 3 4 5 6
value : 1 1 0 1 2 1 2
Actual char: p u r p l e
we can stop here, since nothing else will change