How to check if a string contain only three same repeating characters in PHP

1 Answer

0 votes
function contain_only_three_same_repeating_chars($s) {  
    $len = strlen($s);
    if ($len % 3 != 0) {
        return false;
    }
    for ($i = 0; $i < $len - 3; $i++) {  
         if ($s[$i] != $s[$i + 3]) {  
             return false;  
         }  
    }  
    return true;  
}  
   
   
$s = "wpowpowpowpowpo"; 
   
if (contain_only_three_same_repeating_chars($s)) {
    echo "Yes"; 
}
else {
    echo "No"; 
}




/*
run:

Yes

*/
     

 



answered Jan 9, 2020 by avibootz
edited Dec 23, 2021 by avibootz
...