Workspace Count Number Of Substrings
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Count Number of Substrings visualizer

Learn Count Number of Substrings with a step-by-step Strings visualizer, JavaScript source code and a worked example. Practice fundamentals and parsing.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 7Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": "aba",
  "k": 2
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,k
},s)=>{
let c=0;
for(let i=0;
i<a.length;
i++){
let set=new Set;
for(let j=i;
j<a.length;
j++){
set.add(a[j]);
if(set.size===k)c++;
s("Count substrings with exactly k distinct",{
Array:[...a],Distinct:[...set],Count:[c]
},[i,j])
}
}
return c
};

Execution canvas

Active Changed
STEP 1

Count substrings with exactly k distinct

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Array3 items
Distinct1 items
Count1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Strings · Fundamentals and parsing

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

{
  "a": "aba",
  "k": 2
}

Execution steps

  1. Count substrings with exactly k distinct

Computed output

3
JavaScript reference implementation
const solve = ({
  a,k
},s)=>{
  let c=0;
  for(let i=0;
  i<a.length;
  i++){
    let set=new Set;
    for(let j=i;
    j<a.length;
    j++){
      set.add(a[j]);
      if(set.size===k)c++;
      s("Count substrings with exactly k distinct",{
        Array:[...a],Distinct:[...set],Count:[c]
      },[i,j])
    }
  }
  return c
};

Continue learning

Practice related DSA questions in the same topic.