BackmediumStringsAtlassianRazorpay

Consecutive Nucleotide Count Solution

Problem Statement

Given a string of nucleotide bases consisting of characters 'A', 'C', 'G', and 'T', write a function to count the number of substrings where no two consecutive characters are the same.

Example 1
Input
ATCG
Output
4

Explanation: Step-by-step: Given the input string 'ATCG', we can form substrings like 'A', 'C', 'G', 'T', 'AC', 'GT'. However, we should not count 'AT' and 'CG' as they have consecutive characters. Therefore, the correct count is 4.

Example 2
Input
AA
Output
1

Explanation: Step-by-step: Given the input string 'AA', we can form a single substring 'A'. Therefore, the correct count is 1.

Constraints

  • 1 <= length of DNA sequence <= 1000
  • DNA sequence only contains the characters 'A', 'C', 'G', 'T'
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 Nucleotide Count — Problem Statement & Solution Guide

StringsMediumMixed
TimeO(n^2)
|
SpaceO(1)

Problem Description

Given a string of nucleotide bases consisting of characters 'A', 'C', 'G', and 'T', write a function to count the number of substrings where no two consecutive characters are the same.

Examples

Example 1

Input

ATCG

Output

4

Explanation: Step-by-step: Given the input string 'ATCG', we can form substrings like 'A', 'C', 'G', 'T', 'AC', 'GT'. However, we should not count 'AT' and 'CG' as they have consecutive characters. Therefore, the correct count is 4.

Example 2

Input

AA

Output

1

Explanation: Step-by-step: Given the input string 'AA', we can form a single substring 'A'. Therefore, the correct count is 1.

Constraints

  • 1 <= length of DNA sequence <= 1000
  • DNA sequence only contains the characters 'A', 'C', 'G', 'T'

Optimal Approach & Strategy

A more efficient approach uses a sliding window to generate substrings and checks for alternating nucleotide bases. This approach still has a time complexity of O(n²) in the worst case but with a lower constant factor, making it more efficient in practice.

Brute Force Approach

The brute-force approach involves checking all possible substrings of the DNA sequence, which has a time complexity of O(n²) due to the nested loop structure. This approach is inefficient for large sequences. It checks every substring, resulting in unnecessary comparisons.

Verified Code Solutions

JavaScript Solution
Time: O(n^2)
function countSubstrings(sequence) {
  let count = 0;
  for (let i = 0; i < sequence.length; i++) {
    let prev = sequence[i - 1] || null;
    for (let j = i + 1; j < sequence.length; j++) {
      let substring = sequence.slice(i, j);
      if (substring.length === new Set(substring).size && substring[0] !== substring[1]) {
        count++;
      }
    }
  }
  return count;
}

Asked in Top Tech Interviews

AtlassianRazorpay

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.