PHP Program to Count number of binary strings without consecutive 1's Last Updated : 10 Nov, 2023 Comments Improve Suggest changes Like Article Like Report Write a PHP program for a given positive integer N, the task is to count all possible distinct binary strings of length N such that there are no consecutive 1s. Examples: Input: N = 2Output: 3Explanation: The 3 strings are 00, 01, 10 Input: N = 3Output: 5Explanation: The 5 strings are 000, 001, 010, 100, 101 Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. PHP Program to Count a number of binary strings without consecutive 1's using Dynamic Programming:Let a[i] be the number of binary strings of length i that do not contain any two consecutive 1s and which end in 0. Similarly, let b[i] be the number of such strings which end in 1. We can append either 0 or 1 to a string ending in 0, but we can only append 0 to a string ending in 1. This yields the recurrence relation: a[i] = a[i - 1] + b[i - 1]b[i] = a[i - 1] The base cases of above recurrence are a[1] = b[1] = 1. The total number of strings of length i is just a[i] + b[i]. Below is the implementation of the above approach: PHP <?php // PHP program to count all distinct // binary stringswithout two // consecutive 1's function countStrings($n) { $a[$n] = 0; $b[$n] = 0; $a[0] = $b[0] = 1; for ($i = 1; $i < $n; $i++) { $a[$i] = $a[$i - 1] + $b[$i - 1]; $b[$i] = $a[$i - 1]; } return ($a[$n - 1] + $b[$n - 1])%1000000007; } // Driver Code echo countStrings(3) ; // This code is contributed by nitin mittal ?> Output5Time Complexity: O(N)Auxiliary Space: O(N) Another method:From the above method it is clear that we only want just previous value in the for loop which we can also do by replacing the array with the variable. Below is the implementation of the above approach: PHP <?php function countStrings($n) { $a = 1; $b = 1; for ($i = 1; $i < $n; $i++) { $temp = $a + $b; $b = $a; $a = $temp; } return ($a + $b) % 1000000007; } // Driver program to test the function echo countStrings(3) . PHP_EOL; ?> Output5 Time Complexity: O(N)Auxiliary Space: O(1) Please refer complete article on Count number of binary strings without consecutive 1’s for more details! Comment More infoAdvertise with us Next Article PHP Program to Count number of binary strings without consecutive 1's kartik Follow Improve Article Tags : PHP Similar Reads PHP Program to Count set bits in an integer Write an efficient program to count number of 1s in binary representation of an integer. Examples : Input : n = 6 Output : 2 Binary representation of 6 is 110 and has 2 set bits Input : n = 13 Output : 3 Binary representation of 11 is 1101 and has 3 set bits Recommended: Please solve it on âPRACTICE 2 min read Check if a given String is Binary String or Not using PHP Given a String, the task is to check whether the given string is a binary string or not in PHP. A binary string is a string that should only contain the '0' and '1' characters. Examples:Input: str = "10111001"Output: The string is binary.Input: str = "123456"Output: The string is not binary.Table of 5 min read PHP mb_substr_count() Function The  mb_substr_count() function is an inbuilt function in PHP that counts the occurrence of strings within a given string. Syntax: mb_substr_count($haystack, $needle, $encoding): intParameters: This function accepts three parameters that are described below: $haystack: This is the main parameter wh 1 min read PHP substr_count() Function The substr_count() is a built-in function in PHP and is used to count the number of times a substring occurs in a given string. The function also provides us with an option to search for the given substring in a given range of the index. It is case sensitive, i.e., "abc" substring is not present in 3 min read PHP Program for XOR of Two Binary Strings Given two binary strings, the task is to calculate the XOR of two binary strings in PHP. The XOR operation is a bitwise operation used in computer science and digital electronics. It takes two binary strings as input and produces a binary string as output, where each bit in the output is the result 3 min read PHP Program to Count Zeros and Ones in Binary Representation of a Number Counting the number of zeros and ones in the binary representation of a number is a common task in computer science and programming. In this article, we will explore how to achieve this in PHP using different approaches. Table of Content Using Built-in FunctionsUsing Bitwise OperationsUsing Recursiv 5 min read PHP Program to Count 1's in a sorted binary array Given a binary array sorted in non-increasing order, count the number of 1's in it. Examples: Input: arr[] = {1, 1, 0, 0, 0, 0, 0}Output: 2Input: arr[] = {1, 1, 1, 1, 1, 1, 1}Output: 7Input: arr[] = {0, 0, 0, 0, 0, 0, 0}Output: 0A simple solution is to linearly traverse the array. The time complexit 2 min read PHP Program to Count Sets of 1s and 0s in a Binary Matrix Given a N x M Binary Matrix, count the number of sets where a set can be formed one or more same values in a row or column. Examples: Input: [[ 1, 0, 1 ], [ 0, 1, 0 ]] Output: 8 Explanation: There are six one-element sets (three 1s and three 0s). There are two two - element sets, the first one consi 2 min read How to count the number of words in a string in PHP ? Given a string containing some words and the task is to count number of words in a string str in PHP. In order to do this task, we have the following approaches:Table of ContentUsing str_word_count() MethodUsing trim(), preg_replace(), count() and explode() method. Using trim(), substr_count(), and 4 min read How to Count of Repeating Digits in a Given Number in PHP ? Counting the number of repeating digits in a given number is a common task in programming. This can be useful in various scenarios, such as data analysis or validation. In this article, we will explore different approaches to counting the repeating digits in a given number using PHP.Table of Content 5 min read Like