Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Counting substrings of a string that contains only one distinct letter in JavaScript
We are required to write a JavaScript function that takes in a string as the only argument. The task of our function is to count all the contiguous substrings in the input string that contains exactly one distinct letter.
The function should then return the count of all such substrings.
For example, if the input string is 'iiiji', then the output should be 8 because the desired substrings are: 'i', 'i', 'i', 'ii', 'ii', 'iii', 'j', and 'i'.
How It Works
The algorithm identifies consecutive groups of identical characters and counts all possible contiguous substrings within each group. For a group of n identical characters, the number of possible substrings is n * (n + 1) / 2.
Example Implementation
const str = 'iiiji';
const countSpecialStrings = (str = '') => {
let { length } = str;
let res = length;
if (!length) {
return length;
}
for (let j = 0, i = 1; i < length; ++i) {
if (str[i] === str[j]) {
res += i - j;
} else {
j = i;
}
}
return res;
};
console.log(countSpecialStrings(str));
Output
8
Step-by-Step Breakdown
Let's trace through the example 'iiiji':
const traceExample = (str) => {
console.log("Input string:", str);
console.log("Breaking down substrings:");
// Group 1: "iii" (positions 0-2)
console.log("Group 'iii': substrings are 'i', 'i', 'i', 'ii', 'ii', 'iii' = 6 substrings");
// Group 2: "j" (position 3)
console.log("Group 'j': substring is 'j' = 1 substring");
// Group 3: "i" (position 4)
console.log("Group 'i': substring is 'i' = 1 substring");
console.log("Total: 6 + 1 + 1 = 8 substrings");
};
traceExample('iiiji');
Output
Input string: iiiji Breaking down substrings: Group 'iii': substrings are 'i', 'i', 'i', 'ii', 'ii', 'iii' = 6 substrings Group 'j': substring is 'j' = 1 substring Group 'i': substring is 'i' = 1 substring Total: 6 + 1 + 1 = 8 substrings
Alternative Implementation
Here's a more readable approach that explicitly counts substrings for each group:
const countSpecialStringsAlternative = (str) => {
if (!str.length) return 0;
let totalCount = 0;
let currentGroupSize = 1;
for (let i = 1; i < str.length; i++) {
if (str[i] === str[i - 1]) {
currentGroupSize++;
} else {
// Calculate substrings for current group: n * (n + 1) / 2
totalCount += (currentGroupSize * (currentGroupSize + 1)) / 2;
currentGroupSize = 1;
}
}
// Don't forget the last group
totalCount += (currentGroupSize * (currentGroupSize + 1)) / 2;
return totalCount;
};
console.log(countSpecialStringsAlternative('iiiji'));
console.log(countSpecialStringsAlternative('aabbcc'));
console.log(countSpecialStringsAlternative('abc'));
Output
8 6 3
Conclusion
The key insight is to identify consecutive groups of identical characters and count all possible contiguous substrings within each group. Both implementations achieve O(n) time complexity by making a single pass through the string.
