Wayfair interview question

Given a string, return a boolean value if the strings contains matching brackets Example: Input: "This is [a valid] string" Output: True Input: "This is an [invalid string" Output: False

Interview Answers

Anonymous

12 Mar 2018

Use a stack data structure

2

Anonymous

27 May 2018

function fun(){ $st = "This is [[a valid] st]ring"; $a = str_split($st); $brack_opens = array_keys($a,"["); $brack_close = array_keys($a,"]"); if(count($brack_opens)==count($brack_close)) return "true"; return "false"; } print(fun());

Anonymous

27 Aug 2018

That doesn't work. It would return valid for a string like this "This is ]]a valid[ st[ring"