2147. Number of Ways to Divide a Long Corridor (LeetCode) (solution: C++, C#)



Problem Name: 2147. Number of Ways to Divide a Long Corridor
Problem Link: https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/
Difficulty: Hard
Tag: Math | String | Dynamic Programming
Language: C# | C++
OJ: LeetCode

Algorithm
Initialization:
Initialize variables first and occur as false.
Initialize counters plantCount and sofaCount to 0.
Set a constant mod for modulo operations.
Initialize the result variable answer to 1.

Iterate through the corridor string:
For each character in the corridor string:
If the character is 'P' and it's not the first 'S', and the number of sofas is even, increment plantCount.
Check for 'S' occurrences:

If the character is 'S':
Increment the sofa counter (sofaCount).
If the number of sofas is 2 or more, set occur to true.
If the number of sofas is odd, it's not the first 'S', and there are plants:
Print the current answer (optional step in the provided code).
Update answer by multiplying it with (plantCount + 1) and taking the result modulo mod.
Reset plantCount to 0.

Check final conditions:
If the number of sofas is less than 2 or is odd, set answer to 0.
            Code(C#)
            public class Solution
            {
                public int NumberOfWays(string corridor)
                {
                    bool first = false;
                    bool occur = false;
                    int plantCount = 0;
                    int sofaCount = 0;
                    int mod = 1000000007;
                    long answer = 1;

                    for (int i = 0; i < corridor.Length; i++)
                    {
                        if (corridor[i] == 'P' && first == true && sofaCount % 2 == 0) plantCount++;
                        else if (corridor[i] == 'S')
                        {
                            sofaCount++;
                            if (sofaCount >= 2) occur = true;
                            if (sofaCount % 2 == 1 && first && plantCount > 0)
                            {
                                answer = (answer * (plantCount + 1)) % mod;
                                plantCount = 0;
                            }
                            first = true;
                        }
                    }

                    if (sofaCount < 2 || sofaCount%2 == 1) answer = 0;
                    return (int)answer;
                }
            }

            Code(C++)
            class Solution {
            public:
                int numberOfWays(string corridor) {
                    bool first = false;
                    bool occur = false;
                    int plantCount = 0;
                    int sofaCount = 0;
                    int mod = 1000000007;
                    long long answer = 1;

                    for (int i = 0; i < corridor.length(); i++) {
                        if (corridor[i] == 'P' && first == true && sofaCount % 2 == 0) plantCount++;
                        else if (corridor[i] == 'S') {
                            sofaCount++;
                            if (sofaCount >= 2) occur = true;
                            if (sofaCount % 2 == 1 && first && plantCount > 0) {
                                answer = (answer * (plantCount + 1)) % mod;
                                plantCount = 0;
                            }
                            first = true;
                        }
                    }

                    if (sofaCount < 2 || sofaCount % 2 == 1) answer = 0;
                    return (int)(answer);
                }
            };