= num (g) and num (r) for($i = 0; $rcount && $gcount; $i++) { switch($str[$i]) { case "R": $rcount--; break; case "G": $gcount--; break; case "W": $wcount--; } if($rcount > $gcount || $wcount < $gcount) {break 2;} // too much g after r, too much w after g } $ans = ""; break; } echo $ans."possible"; echo PHP_EOL; $trial--; }