From 5e20cb55e566b7d58fef4b5e803d6f30fd150381 Mon Sep 17 00:00:00 2001 From: chayan das Date: Sun, 14 Dec 2025 23:13:06 +0530 Subject: [PATCH] Create 2147. Number of Ways to Divide a Long Corridor --- .... Number of Ways to Divide a Long Corridor | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 2147. Number of Ways to Divide a Long Corridor 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; + } +};