diff --git a/2147. Number of Ways to Divide a Long Corridor b/2147. Number of Ways to Divide a Long Corridor new file mode 100644 index 0000000..ade299e --- /dev/null +++ b/2147. Number of Ways to Divide a Long Corridor @@ -0,0 +1,60 @@ +class Solution { +public: + // Define the modulo constant + int mod=1e9+7; + + int numberOfWays(string s) { + + int n=s.size(); + int c=0; + + // Step 1: Count total seats + for(int i=0; i=2 && (seat%2)==0) + { + // This 'P' is in a flexible gap, e.g., between S_2 and S_3. + plant++; + } + + // This condition is met when we hit the next odd seat (S_3, S_5, etc.), + // and we have plants in the gap (plant > 0). + if(seat > 2 && seat%2 != 0 && plant > 0) + { + // The gap is closed. The number of ways to place the wall is plant + 1. + // Apply the multiplication principle with modulo operation. + r=((r%mod)*((plant+1)%mod))%mod; + // Reset plant count for the next gap (e.g., between S_4 and S_5) + plant=0; + } + } + + // Step 5: Final Result + return (int)r; + } +};