BackmediumStringsSalesforce

Alternating Character Substrings Solution

Problem Statement

Given a string mealCodes consisting of only two distinct characters, 'V' and 'N', find the total number of substrings where the characters alternate between 'V' and 'N'.

Example 1
Input
VN
Output
3

Explanation: Step-by-step: For input 'VN', we count the substrings 'V', 'VN', and 'N'. The substring 'V' is counted because it's a single character, 'VN' is counted because it's an alternating substring, and 'N' is not counted because it's a single character. Therefore, the total count is 3.

Example 2
Input
NVVN
Output
6

Explanation: Step-by-step: For input 'NVVN', we count the substrings 'N', 'NV', 'V', 'VN', 'N', and 'NV'. The substrings 'N', 'V', and 'N' are counted because they're single characters, 'NV' and 'VN' are counted because they're alternating substrings, and 'V' is not counted because it's a single character. Therefore, the total count is 6.

Constraints

  • 1 <= length of string <= 1000
  • The string will only contain the characters 'V' and 'N'.
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

Alternating Character Substrings — Problem Statement & Solution Guide

StringsMediumMixed
TimeO(n)
|
SpaceO(1)

Problem Description

Given a string mealCodes consisting of only two distinct characters, 'V' and 'N', find the total number of substrings where the characters alternate between 'V' and 'N'.

Examples

Example 1

Input

VN

Output

3

Explanation: Step-by-step: For input 'VN', we count the substrings 'V', 'VN', and 'N'. The substring 'V' is counted because it's a single character, 'VN' is counted because it's an alternating substring, and 'N' is not counted because it's a single character. Therefore, the total count is 3.

Example 2

Input

NVVN

Output

6

Explanation: Step-by-step: For input 'NVVN', we count the substrings 'N', 'NV', 'V', 'VN', 'N', and 'NV'. The substrings 'N', 'V', and 'N' are counted because they're single characters, 'NV' and 'VN' are counted because they're alternating substrings, and 'V' is not counted because it's a single character. Therefore, the total count is 6.

Constraints

  • 1 <= length of string <= 1000
  • The string will only contain the characters 'V' and 'N'.

Optimal Approach & Strategy

A more efficient approach involves iterating through the string once and using two pointers to track the start and end of potential substrings. This approach would have a time complexity of O(n) and a space complexity of O(1), making it more efficient for large inputs.

Brute Force Approach

A brute-force approach would involve generating all possible substrings of the input string and checking each one to see if it alternates between 'V' and 'N'. This would result in a time complexity of O(n^2) due to the nested loops. Additionally, it would require extra space to store the substrings.

Verified Code Solutions

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

Asked in Top Tech Interviews

Salesforce

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.