Special string again (string manipulation) + time and space complexity analysis
Summary
The problem is the following:
Given a string s of length n, determine the number of special substrings within s.
Example: For the string "abcbaba"
A special string is a:
Here is the link to the problem:
Solution
// Complete the substrCount function below.
private static long substrCount (int n, String s) {
long count = 0;
//step 1: count substrings with all identical characters
int i = 0;
while(i < n) {
int charCount = 1; // keeps track of consecutive identical characters
while( i+1 < n && s.charAt(i) == s.charAt(i+1)) {
i++;
charCount++;
}
//Add the number of strings that can be formed from this block
/*
* If you have charCount consecutive identical characters,
* the number of substrings that can be formed from this block is
* given by the sum of the first charCount natural numbers.
*/
count += (charCount * (charCount + 1)) / 2 ;
i++;
}
//Step 2: Count substrings with one different middle character
/*This loop iterates over each character in the string,
* considering each one as a potential middle character
*/
for(i = 1; i < n ; i++) {
int charCount = 1;
/* charCount: Represents the number of characters
* on each side of the middle character that are the same */
while ( i - charCount >= 0 // The left side of the substring is within bounds.
&& i + charCount < n // The right side of the substring is within bounds.
&& s.charAt(i - 1) != s.charAt(i)
/*
* Checks that the middle character (at index i) is different from
* the character immediately to the left of the middle (at index i - 1).
*/
&& s.charAt(i - charCount) == s.charAt(i - 1 )
/* Checks that the character to the left of the middle character
* (at index i - charCount)
* matches the character immediately to the left of the middle
* (at index i - 1).
*/
&& s.charAt(i + charCount) == s.charAt(i -1 )
/*
* Checks that the character to the right of the middle character
* (at index i + charCount)
* also matches the character immediately to the left of the middle.
* /
) {
count++;
charCount++;
}
}
return count;
}
Step-by-step explanation
int i = 0;
while (i < n) {
int charCount = 1;
while (i + 1 < n && s.charAt(i) == s.charAt(i + 1)) {
i++;
charCount++;
}
count += (charCount * (charCount + 1)) / 2;
i++;
}
- Outer loop: Iterate over the string s using index i
- Inner loop: Count consecutive identical characters starting from s[i]
- "charCount" keeps track of the number of consecutive identical characters.
- Calculate substrings for 'charCount' consecutive identical characters, the number of substrings
The number of substrings is given by the formula: (charCount?(charCount+1))/2
This formula calculates the sum of the first charCount natural numbers,
representing the total number of substrings that can be formed from these characters.
for(i = 1; i < n ; i++) {
int charCount = 1;
/* charCount: Represents the number of characters
* on each side of the middle character that are the same */
while ( i - charCount >= 0 // The left side of the substring is within bounds.
&& i + charCount < n // The right side of the substring is within bounds.
&& s.charAt(i - 1) != s.charAt(i)
/*
* Checks that the middle character (at index i) is different from
* the character immediately to the left of the middle (at index i - 1).
*/
&& s.charAt(i - charCount) == s.charAt(i - 1 )
/* Checks that the character to the left of the middle character
* (at index i - charCount)
* matches the character immediately to the left of the middle
* (at index i - 1).
*/
&& s.charAt(i + charCount) == s.charAt(i -1 )
/*
* Checks that the character to the right of the middle character
* (at index i + charCount)
* also matches the character immediately to the left of the middle.
* /
) {
count++;
charCount++;
}
}
- We start from '1' because the middle character must have characters on both sides
- Bounds check: Ensure the substring is within the string's boundaries
- Middle Character check: Ensure the character at the current index 'i' (middle character) is different from its immediate left character 's[i-1]'
- Symmetry Check: Ensure the character to the left and right of the middle character (at distances defined by charCount) are the same as s[i-1]
- If all conditions are met, increment 'count' and 'charCount'
Time and Space complexity analysis
领英推荐
Step 1: Count substrings with all identical characters
The time complexity of step one is O(n)
Step 2: Count substrings with one different middle character
So the time complexity of step 2 is O(n)
Combined time complexity:
Both steps individually have a time complexity of O(n). Therefore, the combined time complexity of the 'substrCount' function is: O(n)
Space complexity:
Step 1: Count substrings with all identical characters
The function uses a few integer variables ('count', 'i', 'charCount')
These variables use constant space, O(1)
Step 2: Count substrings with one different middle character
The function uses a few integer variables ('i', 'charCount')
These variables use constant space, O(1)
Total space complexity: O(1)
Thank you for taking the time to read this article.
Senior Software Engineer at LatticeFlow AI
7 个月Good solution, but here's an even better follow-up: Take the same definition of "special" with the same constrains and everything, but this time you don't have to count how many are the special substrings, but instead answer Q queries (1 <= q <= 10^6) of type: "Given to indices, left and right (0 <= left <= right < n), verify if the subsring x[left...right] is special."