---------------LeetCode Progress Update Solved: Jewels and Stones (771) * Problem Insight: Given two strings, determine how many stones are also jewels. * Approach: • Stored all jewel characters in a HashSet for fast lookup • Iterated through stones and counted matches • Achieved efficient O(n) time complexity * Key Learning: Using the right data structure (like HashSet) can simplify logic and improve performance significantly. #LeetCode #DSA #Java #HashSet #ProblemSolving
LeetCode 771 Jewels and Stones Solution
More Relevant Posts
-
🚀 LeetCode Challenge 30/50 💡 Approach: HashSet (Sequence Start Detection) Sorting would give O(n log n) — but the problem demands O(n)! The trick? Use a HashSet and only start counting from the BEGINNING of each sequence! 🔍 Key Insight: → Add all numbers to a HashSet (O(1) lookup) → For each number, check if (num - 1) exists in set → If NOT → it's the start of a new sequence! → Count upward (num+1, num+2...) while consecutive numbers exist → Update maxLength at each sequence end 📈 Complexity: ❌ Sorting approach → O(n log n) Time ✅ HashSet approach → O(n) Time, O(n) Space Each number is visited at most twice across all sequences! 🎉 Day 30/50 — 60% done and still going strong! The best optimizations come from asking: ‘What information can I precompute?’ A HashSet turned O(n log n) into O(n)! 🔥 #LeetCode #DSA #HashSet #Java #ADA #PBL2 #LeetCodeChallenge #Day30of50 #CodingJourney #ComputerEngineering #AlgorithmDesign #LongestConsecutiveSequence
To view or add a comment, sign in
-
-
Day: 97/365 📌 LeetCode POTD: Minimum Distance to the Target Element Easy Key takeaways/Learnings from this problem: 1. This one shows that sometimes the simplest approach—just scanning the array—is more than enough. 2. Key learning: track the minimum absolute distance on the go instead of storing unnecessary data. 3. Good reminder that not every problem needs fancy algorithms, clarity beats complexity. #POTD #365DaysOfCode #DSA #Java #ProblemSolving #LearningInPublic #Consistency 🥷
To view or add a comment, sign in
-
-
🚀 𝐃𝐚𝐲 86/100 – 𝐋𝐨𝐧𝐠𝐞𝐬𝐭 𝐂𝐨𝐧𝐬𝐞𝐜𝐮𝐭𝐢𝐯𝐞 𝐒𝐞𝐪𝐮𝐞𝐧𝐜𝐞 Today’s problem was Longest Consecutive Sequence — a great example of optimizing from brute force to an efficient solution using hashing. 🔍 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: Sorting would take O(n log n), but we can solve this in O(n) using a HashSet. 💡 𝐂𝐨𝐫𝐞 𝐈𝐝𝐞𝐚: Store all elements in a set for O(1) lookup Only start counting when the current number is the start of a sequence 𝐖𝐡𝐲 𝐢𝐭 𝐰𝐨𝐫𝐤𝐬? This ensures we only count each sequence once, avoiding unnecessary repeated work. ⚡ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Add all elements to a HashSet Iterate through the set: If (num - 1) doesn’t exist → start a sequence Keep increasing and count streak Track maximum length ⏱️ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) 📦 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) #Day86 #100DaysOfCode #Java #DSA #LeetCode #HashSet #CodingJourney
To view or add a comment, sign in
-
-
💡 LeetCode 191 — Number of 1 Bits (Hamming Weight) Recently solved an interesting bit manipulation problem that highlights the power of low-level optimization 🚀 🔍 Problem Statement: Given an unsigned integer, count the number of set bits (1s) in its binary representation. 🧠 Key Insight: Instead of checking every bit individually, we can use a clever trick: 👉 n & (n - 1) removes the rightmost set bit in each operation. This allows us to count only the set bits, making the solution more efficient. ⚙️ Approach Used (Brian Kernighan’s Algorithm): Initialize a counter Repeatedly apply n = n & (n - 1) Increment count until n becomes 0 📈 Time Complexity: O(k), where k = number of set bits (faster than checking all 32 bits) 📦 Space Complexity: O(1) ✨ Why this problem is important: Strengthens understanding of bit manipulation Frequently asked in interviews Useful in low-level optimization and system design 💬 Takeaway: Sometimes the best solutions come from understanding how data is represented at the binary level. Small tricks can lead to big optimizations! #LeetCode #DSA #CodingInterview #Java #BitManipulation #ProblemSolving #TechJourney
To view or add a comment, sign in
-
-
LeetCode Challenge – Day 51 Today I solved the Diameter of Binary Tree problem. Problem Insight: The task is to find the length of the longest path between any two nodes in a binary tree. The path does not necessarily pass through the root. Approach: Used Depth First Search (DFS) to calculate the height of each subtree For every node: Calculated left subtree height Calculated right subtree height Updated the maximum diameter as the sum of left and right heights Returned the maximum diameter found during traversal Key Learning: Tree problems often combine multiple concepts. Here, calculating height and updating diameter together in a single traversal makes the solution efficient. Complexity: Time: O(n) Space: O(h) This problem helped me understand how recursion can be used to compute multiple values in a single traversal. #LeetCode #Java #DSA #CodingJourney
To view or add a comment, sign in
-
🚀 DSA Journey — LeetCode Practice 📌 Problem: Binary Tree Level Order Traversal (LeetCode 102) 💻 Language: Java 🔹 Approach: To solve this problem, I used Breadth-First Search (BFS) with a Queue to traverse the binary tree level by level. • Start by adding the root node into a queue • Use queue.size() to track the number of nodes at the current level • Process all nodes of that level using a loop • Store each level’s values in a separate list • Add left and right child nodes into the queue for the next level • Add each level list into the final result This approach efficiently groups nodes by levels and naturally fits the problem requirement. ⏱ Time Complexity: O(n) 🧩 Space Complexity: O(n) 📖 Key Learning: This problem strengthened my understanding of BFS, Queue operations, and level-wise tree traversal. It also reinforced how queue.size() helps process one complete level at a time — a very useful pattern for many tree problems 💡 #DSA #Java #LeetCode #ProblemSolving #CodingJourney #LearningInPublic #BinaryTree #BFS #Queue
To view or add a comment, sign in
-
-
🚀 Day 46 of #DSA ✅ Solved: Search a 2D Matrix (LeetCode 74) Today’s problem was about applying Binary Search in a 2D matrix. 💡 Key Insight: Instead of searching row by row, we can treat the matrix as a flattened sorted array and apply binary search directly. ⚙️ Approach: - Consider matrix as 1D - Use index mapping → row = mid / cols, col = mid % cols - Apply standard binary search ⚡ Time Complexity: O(log (m × n)) 📚 Key Learning: Sometimes changing the perspective (2D → 1D) makes problems much simpler and more efficient. Leveling up with Binary Search 🔥 #Day46 #BinarySearch #LeetCode #Java #DSA #CodingJourney #ProblemSolving #100DaysOfCode #Tech
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode | Day 49 🔍 Solved: Largest Number Today I worked on an interesting problem where the goal was to arrange numbers such that they form the largest possible number. 💡 Key Insight: Instead of normal sorting, we compare numbers based on their string combinations (like "ab" vs "ba"). 📌 Approach: ✔ Converted integers to strings ✔ Used a custom comparator for sorting ✔ Compared (a + b) and (b + a) ✔ Sorted in descending order based on best combination ✔ Handled edge case when all elements are 0 Why this works: By comparing two numbers in different orders, we ensure that the arrangement always produces the maximum possible value when concatenated. 🎯 What I Learned: This problem taught me that sorting can go beyond numbers—custom logic and string manipulation are powerful tools in problem solving. #Java #DSA #LeetCode #Sorting #Comparator #CodingJourney #ProblemSolving #TechSkills 🚀
To view or add a comment, sign in
-
-
🚀 𝗗𝗮𝘆 - 46/60 Problem: Print Anagrams Together 🔍 Learned: Grouped strings that are anagrams by using a HashMap, where the key is the sorted version of each string. All strings with the same sorted key belong to the same group. 😅 Struggles: Initially confusing part was deciding the grouping logic — realized that sorting each string is the simplest way to normalize anagrams into a common key. 🧠 Key Learning: Anagrams share the same characters → so sorted string acts as a unique identifier. Use HashMap<String, ArrayList> to collect groups. Order of insertion is preserved inside each group, so original order is maintained. 📦 Concepts Used: #HashMap #Strings #Sorting #Anagrams #DataStructures #DSA Learning how to transform a problem into a key-based grouping problem makes it much easier to solve. 🚀 #Java #CodingJourney #ProblemSolving #DSA
To view or add a comment, sign in
-
-
🚀 Day 15 of LeetCode Problem Solving Solved today’s problem — LeetCode #49: Group Anagrams 💻🔥 ✅ Approach: HashMap + Sorting ⚡ Time Complexity: O(n * k log k) 📊 Space Complexity: O(n * k) The task was to group strings that are anagrams of each other. 👉 I used a HashMap where: Key = sorted version of string Value = list of anagrams 💡 Key Idea: If two strings are anagrams, their sorted form will be the same. 👉 Core Logic: Convert string → char array Sort the array Use sorted string as key Store original string in map 💡 Key Learning: Transforming data (like sorting strings) can help in identifying patterns and grouping efficiently. Consistency is the key — learning something new every day 🚀 #Day15 #LeetCode #DSA #Java #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development