Introduction
The Repeated Pattern Check problem is a string-based programming problem.
In this problem, we are given a string containing only two characters, x and y.
Our task is to check whether the string can be divided into one or more groups following the pattern xⁿyⁿ.
Here, n represents the number of consecutive x characters, and the same number of y characters must follow them.
In simple terms, every valid group must contain one or more x characters followed by the same number of y characters.
For example:
Input | Result |
|---|---|
xy | Valid |
xxyy | Valid |
xxxyyy | Valid |
xxy | Invalid |
xyx | Invalid |
The important point is that the string can contain multiple valid groups.
For example, xxyyxy can be divided into two groups:
xxyy | xy
Both groups follow the required pattern, so the complete string is valid.
Understanding the Pattern
Let's look at some valid groups.
Pattern xy
There is one x and one y.
Therefore, the group is valid.
Pattern xxyy
There are two x characters and two y characters.
Therefore, the group is valid.
Pattern xxxyyy
There are three x characters and three y characters.
Therefore, the group is valid.
Multiple Groups
The pattern can repeat multiple times.
Consider the following string:
xxyyxy
It can be divided into:
xxyy | xy
The first group contains two x characters and two y characters.
The second group contains one x character and one y character.
Both groups are valid, so the complete string is valid.
C# Implementation
We can solve the problem using two counters and a single pass through the string.
class Solution
{
public bool findPattern(string s)
{
int i = 0;
int n = s.Length;
while (i < n)
{
int xCount = 0;
while (i < n && s[i] == 'x')
{
xCount++;
i++;
}
if (xCount == 0)
return false;
int yCount = 0;
while (i < n && s[i] == 'y')
{
yCount++;
i++;
}
if (xCount != yCount)
return false;
}
return true;
}
}Step-by-Step Explanation
Step 1: Initialize the Variables
int i = 0;
int n = s.Length;The variable i represents the current position in the string.
The variable n stores the total length of the string.
For example, if the input is xxyyxy, the length is 6.
Step 2: Start the Main Loop
while (i < n)The loop continues until every character in the string has been processed.
We need a loop because the string may contain multiple groups.
For example:
xxyy | xy | xxxyyy
Each group must be checked separately.
Step 3: Count Consecutive x Characters
int xCount = 0;
while (i < n && s[i] == 'x')
{
xCount++;
i++;
}This loop counts the consecutive x characters starting from the current position.
For xxyy, the program finds two x characters.
After processing them:
xCount = 2
The index then points to the first y character.
Step 4: Check Whether an x Was Found
if (xCount == 0)
return false;Every valid group must start with at least one x character.
Consider:
yyxx
The first character is y, so the program cannot create a valid group.
Therefore, the string is invalid.
Step 5: Count Consecutive y Characters
int yCount = 0;
while (i < n && s[i] == 'y')
{
yCount++;
i++;
}After counting the x characters, the program counts the consecutive y characters.
For xxyy, two y characters are found.
The values are now:
xCount = 2
yCount = 2
Step 6: Compare the Counts
if (xCount != yCount)
return false;The number of x characters must be exactly the same as the number of y characters.
For example:
xxyy
contains two x characters and two y characters, so it is valid.
However:
xxxyy
contains three x characters and two y characters.
Since the counts are different, the string is invalid.
Step 7: Continue With the Next Group
If the counts are equal, the program continues from the current position.
For example:
xxyyxy
The first group is:
xxyy
After processing it, the program continues with:
xy
The second group is also valid.
When the complete string has been processed, the function returns true.

Join the conversation! Your thoughts help the community grow.