BackmediumStringsMicrosoftSalesforce

Consecutive Character Blocks Solution

Problem Statement

Given a string of characters, count the number of blocks where a character is immediately followed by the same character, ignoring isolated characters without consecutive duplicates.

Example 1
Input
aaabbbccc
Output
2

Explanation: Step-by-step: 1. Initialize a counter for consecutive blocks. 2. Iterate over the string, checking for consecutive characters. 3. If a character is the same as the previous one, increment the counter. 4. If a character is different from the previous one, reset the counter. 5. After iterating over the entire string, return the counter.

Example 2
Input
abc
Output
0

Explanation: Step-by-step: 1. Initialize a counter for consecutive blocks. 2. Iterate over the string, checking for consecutive characters. 3. Since 'a' and 'b' are not consecutive, reset the counter. 4. Since 'b' and 'c' are not consecutive, reset the counter. 5. After iterating over the entire string, return the counter.

Constraints

  • 1 <= transmission string length <= 1000
  • Transmission string only contains uppercase English letters
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Consecutive Character Blocks — Problem Statement & Solution Guide

StringsMediumMixed
TimeO(n)
|
SpaceO(1)

Problem Description

Given a string of characters, count the number of blocks where a character is immediately followed by the same character, ignoring isolated characters without consecutive duplicates.

Examples

Example 1

Input

aaabbbccc

Output

2

Explanation: Step-by-step: 1. Initialize a counter for consecutive blocks. 2. Iterate over the string, checking for consecutive characters. 3. If a character is the same as the previous one, increment the counter. 4. If a character is different from the previous one, reset the counter. 5. After iterating over the entire string, return the counter.

Example 2

Input

abc

Output

0

Explanation: Step-by-step: 1. Initialize a counter for consecutive blocks. 2. Iterate over the string, checking for consecutive characters. 3. Since 'a' and 'b' are not consecutive, reset the counter. 4. Since 'b' and 'c' are not consecutive, reset the counter. 5. After iterating over the entire string, return the counter.

Constraints

  • 1 <= transmission string length <= 1000
  • Transmission string only contains uppercase English letters

Optimal Approach & Strategy

An optimized approach involves using a single loop to iterate through the transmission string, keeping track of the current character and the count of consecutive occurrences. This approach allows us to identify signal blocks in a single pass through the string, resulting in a much more efficient solution. The optimized approach can be implemented using a simple loop and conditional statements.

Brute Force Approach

A brute-force approach would involve comparing each character with every other character in the string, resulting in a time complexity of O(n²). However, this approach is inefficient and would not be suitable for large transmission strings. The brute-force approach can be implemented using nested loops to compare characters and count signal blocks.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function countBlocks(s) {
  let count = 0, i = 0;
  while (i < s.length) {
    if (i < s.length - 1 && s[i] === s[i + 1]) {
      let j = i + 1;
      while (j < s.length && s[j] === s[i]) {
        j++;
      }
      count++;
      i = j;
    } else {
      i++;
    }
  }
  return count;
}

Asked in Top Tech Interviews

MicrosoftSalesforce

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.