= num (g) and num (r) for($i = 0; $rcount || $gcount; $i++) // check as long as far there's r or g so that w lacks (such like WGGwwwRR) { 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--; }