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.
Complete Dry Run
Let's take the following example:
xxyyxy
The string contains:
Position | Character |
|---|---|
0 | x |
1 | x |
2 | y |
3 | y |
4 | x |
5 | y |
The program starts at position 0.
First Group
The program counts two x characters.
Then it counts two y characters.
The result is:
xCount = 2
yCount = 2
The first group is valid.
The program then continues from position 4.
Second Group
The program finds one x character and one y character.
The result is:
xCount = 1
yCount = 1
The second group is also valid.
All characters have now been processed.
Therefore, the function returns true.
So:
xxyyxy = true
Important Test Cases
Input | Output | Reason |
|---|---|---|
xy | true | One x and one y |
xxyy | true | Two x and two y |
xxyyxy | true | Contains two valid groups |
xxxyyy | true | Three x and three y |
xyx | false | Final x has no matching y |
xxy | false | Unequal counts |
yyxx | false | Does not start with x |
xxyyy | false | Unequal counts |
xyy | false | One x and two y |
xxxyyyxy | true | Both groups are valid |
Why Checking Total Counts Is Not Enough
A common mistake is to count all x and y characters in the entire string and compare their totals.
Consider:
xyyx
The string contains:
two x characters
two y characters
The total counts are equal, but the string is still invalid.
The string cannot be divided completely into groups following the required pattern.
The first group could be:
xy
but the remaining characters are:
yx
This remaining group does not start with x.
Therefore, the string is invalid.
This is why we must validate every group rather than checking only the total number of x and y characters.
Time and Space Complexity
Time Complexity
The algorithm processes every character only once.
The index always moves forward and never moves backward.
Therefore, the time complexity is O(n), where n is the length of the string.
Auxiliary Space Complexity
The algorithm uses only a few variables to store counters and the current index.
It does not create another array, list, or string.
Therefore, the auxiliary space complexity is O(1).
Common Mistakes
Checking Only the Total Number of Characters
Equal totals do not guarantee that the string follows the required pattern.
For example:
xyyx
contains two x characters and two y characters, but it is invalid.
Allowing a Group Without x
A valid group must contain at least one x character.
Therefore:
yy
is invalid.
Forgetting to Compare the Counts
Counting the characters without comparing them can allow invalid strings such as:
xxxyy
There are three x characters but only two y characters, so the group is invalid.
Not Checking the Entire String
Checking only the first group is not enough.
For example:
xxyyxx
contains a valid first group:
xxyy
But the remaining characters are:
xx
There are no matching y characters, so the complete string is invalid.
The complete string must be processed before returning true.
Practice Yourself
Try to determine the result for each input.
Question 1
xy
Question 2
xxyyxy
Question 3
xyyx
Question 4
xxxyyyx
Answers
xy → true
xxyyxy → true
xyyx → false
xxxyyyx → false
Summary
The main idea behind this problem is to validate the string group by group.
The algorithm follows these steps:
Count consecutive x characters.
Make sure at least one x was found.
Count the consecutive y characters.
Compare the number of x and y characters.
Return false if the counts are different.
Continue with the next group.
Return true only after the complete string has been validated.
The important point is that this is not simply a character-counting problem. The order and grouping of the characters matter.
By scanning the string once and validating each group, we can solve the problem efficiently with O(n) time and O(1) auxiliary space.


Join the conversation! Your thoughts help the community grow.