Skip to main content

Posts

Showing posts from August, 2026

Frequencies of Characters By Brute Force Vs Hashing

Character Frequencies — Hashing vs Brute Force Java · DSA · Character Hashing Character frequencies — hashing vs brute force Same problem, two solutions, wildly different scaling. Type a string below and watch both approaches work in real time. 🔤 The 26-bucket hasher Every lowercase letter maps straight to hash[ch - 'a'] — no scanning required. String input Query characters Hash it 01 The two implementations Both take a string and answer "how many times does character X appear?" — but they get there very differently. Hashing.java BruteForce.java import java.util.Scanner; public class CharacterFrequenciesByHashing { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); char [] arr = new char [n]; int [] has...

Frequencies of Array By Hashing Technique (Optimized Approach)

Frequencies by Hashing — an interactive walkthrough Java · DSA · Hashing Counting frequencies by hashing — and where the array should live A working int[] hash-array solution, traced against three inputs, plus the actual reason people move big arrays outside main() . Play with the bucket hasher below before you read a line of code. 🪣 The bucket hasher Type an array — watch it drop into indexed buckets, live. Array (space separated) Queries (space separated) Hash it Every value becomes an index. hash[value]++ drops a ball in that bucket — no comparisons, no searching, just direct addressing. That's the whole trick. 01 The working solution This is a correct, minimal frequency counter. It sizes hash to n + 1 , which works cleanly as long as every value fits within that range. FrequenciesByHa...