Workspace Valid Anagram
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Valid Anagram visualizer

Learn Valid Anagram 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 / 15Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": "anagram",
  "b": "nagaram"
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,b
},s)=>{
let m=new Map;
for(let x of a){
m.set(x,(m.get(x)||0)+1);
s("Count first word",{
Counts:[...m].map(p=>p.join(":"))
})
}
for(let x of b){
m.set(x,(m.get(x)||0)-1);
s("Subtract second word",{
Counts:[...m].map(p=>p.join(":"))
})
}
return [...m.values()].every(x=>!x)
};

Execution canvas

Active Changed
STEP 1

Count first word

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

Counts1 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": "anagram",
  "b": "nagaram"
}

Execution steps

  1. Count first word
  2. Subtract second word

Computed output

true
JavaScript reference implementation
const solve = ({
  a,b
},s)=>{
  let m=new Map;
  for(let x of a){
    m.set(x,(m.get(x)||0)+1);
    s("Count first word",{
      Counts:[...m].map(p=>p.join(":"))
    })
  }
  for(let x of b){
    m.set(x,(m.get(x)||0)-1);
    s("Subtract second word",{
      Counts:[...m].map(p=>p.join(":"))
    })
  }
  return [...m.values()].every(x=>!x)
};

Continue learning

Practice related DSA questions in the same topic.