Day90 Word Pattern with HashMap and Set

🚀 𝐃𝐚𝐲 90/100 – 𝐖𝐨𝐫𝐝 𝐏𝐚𝐭𝐭𝐞𝐫𝐧  Today’s problem was Word Pattern — a great exercise on HashMap + Set usage and understanding bijection. 🔍 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: We need to ensure a one-to-one mapping between pattern characters and words. 💡 𝐂𝐨𝐫𝐞 𝐈𝐝𝐞𝐚: Each character maps to exactly one word Each word maps to exactly one character (no duplicates mapping)  𝐖𝐡𝐲 𝐢𝐭 𝐰𝐨𝐫𝐤𝐬? HashMap → to store character → word mapping HashSet → to track already mapped words ensures a valid bijection. ⚡  𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Split string into words If lengths mismatch → return false Traverse pattern: If mapping exists → validate Else → check if word already used Return true if all checks pass ⏱️ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) 📦 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) #Day90 #100DaysOfCode #Java #DSA #LeetCode #HashMap #CodingJourney #Consistency

  • graphical user interface, text, email

To view or add a comment, sign in

Explore content categories