← TCS bank

TCS · Question 2 · Strings

Check if Two Strings are Anagrams

StringsC++code⏱ O(n log n)💾 O(n)
Service-based

Problem

Determine if two strings contain the same characters in any order.

Approach

Sort both strings and compare, or count letter frequencies.

Time O(n log n)Space O(n)

How it works

  1. 1Reject on length mismatch.
  2. 2Sort both and compare.

Sample input / output

Input:
a = "listen", b = "silent"
Output:
true

Constraints

Lowercase letters.

Solution — read it, then walk it

Time O(n log n)Space O(n)Returns 1source · C++
Scroll sideways for wide charts — the view follows the active step.
STARTinclude <iostream>include <string>include <algorithm>using namespace stdbool areAnagrams(string a,string b)DEFINE main()cout << boolalpha <<areAnagrams("listen","silent") << endlRETURN 0END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/9

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits