🚀 Just solved LeetCode 648 – Replace Words. 🟩 What was the problem? You’re given a list of root words and a sentence. For every word in the sentence, you need to replace it with the shortest root that matches its prefix. If multiple roots match, you pick the smallest one. If none match, you leave the word as it is. 🛠️ What was my approach? • Grouped all roots in a HashMap based on their first character • Split the sentence into individual words • For each word, checked only the relevant roots (same starting character) • Picked the shortest matching root if multiple matched • Built the final updated sentence using a StringBuilder This avoided checking every root against every word and kept things pretty efficient. 📚 What I learned Working with strings becomes simpler when you organize the data around predictable patterns. Grouping roots by their first letter saved a lot of unnecessary comparisons. Small structural changes make the whole solution cleaner. #LeetCode #Java #DSA #CodingPractice #ProblemSolving
Solved LeetCode 648 – Replace Words using HashMap and StringBuilder
More Relevant Posts
-
🚀 Just solved LeetCode 30 – Substring With Concatenation of All Words. 🧩 What was the problem? Given a string and a list of words (all same length), you have to find every starting index where all the words appear together as a continuous substring, in any order. Basically a sliding-window puzzle with fixed word sizes and frequency matching. 🛠️ What was my approach? • Counted all words using a map • Used multiple sliding windows based on word length • Expanded the window word by word • If a word appeared more than needed, I moved the left pointer forward • Added the index when the window matched every word exactly The key was treating the string in equal chunks, not character by character. 📚 What I learned Working with strings becomes much easier when you break the problem into predictable pieces. Managing window boundaries is the real challenge here, and walking through tiny examples helps more than you think. #LeetCode #Java #DSA #CodingPractice #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 73 of #100DaysOfCode Today I solved LeetCode 3346 – Maximum Frequency of an Element After Performing Operations I 🧩 This problem was all about logical range manipulation — figuring out how many elements could be converted into the same number when each element can be modified by ±k, but with a limited number of allowed operations. At first, it felt similar to the “most frequent element” sliding window problem, but the twist here was controlling how many elements can be changed, not just the total cost. After a few failed attempts (and a battle with edge cases 😅), I finally implemented a correct solution using the difference array + prefix sum approach. 💡 Key Takeaways: Think in terms of ranges, not just differences. Prefix-sum (difference map) logic is incredibly powerful for interval counting. Always recheck constraints — “number of operations” vs “total cost” makes a huge difference! Here’s a snippet from my final Java solution: int achievable = Math.min(running, same + numOperations); ans = Math.max(ans, achievable); It’s small details like these that make problem-solving such a rewarding process 💪 #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #DSA #LearningEveryday
To view or add a comment, sign in
-
-
✨ Day 54 of 100: Reverse Nodes in k-Group ✨ Today’s challenge was LeetCode 25 – Reverse Nodes in k-Group 🔁 🧩 Problem: Given a linked list, reverse the nodes of the list k at a time and return its modified list. If the number of nodes is not a multiple of k, the remaining nodes should remain as-is. 💡 Key Takeaways: 🔹 Strengthened understanding of linked list manipulation and pointer handling. 🔹 Practiced breaking the list into segments of size k and reversing each group independently. 🔹 Learned how to manage head and tail connections between reversed and remaining parts. 🔹 Reinforced problem-solving using dummy nodes to simplify list operations. 🧠 Approach: 1️⃣ Count the total nodes to know how many full groups of k exist. 2️⃣ For each group, reverse k nodes iteratively. 3️⃣ Connect the reversed group to the next segment. 🧭 Insight: This problem beautifully illustrates how iteration with precise pointer movement can replace recursion while still achieving elegant reversals in linked lists. #100DaysOfCode #Day53 #LeetCode #Java #LinkedList #CodingJourney #DSA #ProblemSolving
To view or add a comment, sign in
-
-
✨ Day 57 of 100: Rotate List ✨ Today’s challenge was LeetCode 61 – Rotate List 🔄 Problem: Given the head of a linked list, rotate the list to the right by k places. Example: Input: head = [1,2,3,4,5], k = 2 Output: [4,5,1,2,3] Key Idea: To rotate the list: 1️⃣ Find the length of the linked list. 2️⃣ Connect the tail to the head to make it circular. 3️⃣ Calculate the new head position as length - (k % length) steps from the start. 4️⃣ Break the circle at the right point to get the rotated list. 💡 Key Takeaways: Strengthened understanding of linked list traversal and manipulation. Learned to handle edge cases like k larger than the list length efficiently using modulo. Practiced converting a circular list back into a normal one at the right node. 💬 Every day of this challenge reinforces how small logical tweaks — like using % to handle rotations — can make code elegant and efficient. #100DaysOfCode #LeetCode #Java #LinkedList #CodingChallenge #DSA #ProblemSolving #WomenWhoCode
To view or add a comment, sign in
-
-
🚀 Day 60 of My LeetCode Challenge Problem: Defanging an IP Address — LeetCode #1108 Today’s problem was a straightforward yet insightful string manipulation task. The goal was to replace every . in an IP address with [.]. 💡 Approach: Used a StringBuilder to efficiently construct the modified string: • Traversed each character in the given address. • When encountering a ., appended [.] to the builder. • For all other characters, appended them directly. • Converted the StringBuilder to a string and returned the result. This method ensures both clarity and efficiency while avoiding unnecessary string object creation. ⏱️ Time Complexity: O(n) — Single traversal of the string. 💾 Space Complexity: O(n) — New string built proportional to input length. Each day adds a small step toward mastering clean code and algorithmic thinking 💪 #LeetCode #Day60 #LeetCode1108 #Java #CodingChallenge #ProblemSolving #StringManipulation
To view or add a comment, sign in
-
-
🚀 Just Solved LeetCode #110 — Balanced Binary Tree 📘 Problem: Given the root of a binary tree, determine if it is height-balanced — that is, for every node, the height difference between the left and right subtree should not exceed 1. Example: Input → [3,9,20,null,null,15,7] Output → true 🧠 My Approach: I used a bottom-up recursive approach to check balance efficiently. 1️⃣ For each node, I calculated the height of its left and right subtrees. 2️⃣ If any subtree was unbalanced, I returned -1 immediately to stop further checks. 3️⃣ If the height difference between left and right subtrees was greater than 1, I marked the tree as unbalanced. 4️⃣ Otherwise, I returned the height of the current node as 1 + max(left, right). This approach ensures every node is visited only once — making it O(n) in time complexity. 💡 What I Learned: ✅ The difference between top-down and bottom-up recursion in tree problems ✅ How to optimize recursion by early termination when imbalance is detected ✅ Strengthened my understanding of recursive depth and height calculation in binary trees #LeetCode #Java #DSA #BinaryTree #CodingJourney
To view or add a comment, sign in
-
-
📌 LeetCode Day 56 — #2. Add Two Numbers Problem Description: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit. Your task is to add the two numbers and return the sum as a linked list in the same reversed order. Approach: Initialize Pointers: Use a dummy node to simplify list creation and a current pointer. Iterate Both Lists: Traverse l1 and l2, adding corresponding digits along with a carry. Handle Carry: If the sum ≥ 10, carry over to the next node. Attach Remaining Carry: After traversal, if carry > 0, create a new node. Return Result: Return dummy.next as the head of the result list. Complexity Analysis: Time Complexity: O(max(n, m)) — traverse both lists once (n and m are lengths of the lists). Space Complexity: O(max(n, m)) — new linked list is created for the result. Hashtags: #LinkedList #Math #LogicBuilding #LeetCode100Days #Day56 #Java #ProblemSolving
To view or add a comment, sign in
-
-
🎯 LeetCode 2390 – Removing Stars From a String Today I solved another interesting Stack / String Manipulation problem! 💡 Problem Summary: You’re given a string s containing lowercase letters and *. Each * means you must remove the closest non-star character to its left along with the star itself. Your task is to return the final string after all operations. 🧠 Approach: I used a StringBuilder as a stack to simulate the process. Traverse through the string. If the current character is not *, append it. If it’s *, remove the last added character. Finally, return the resulting string. ⏱️ Time Complexity: O(n) 💾 Space Complexity: O(n) #LeetCode #Java #DSA #Stack #CodingJourney #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 20 of #Blind75 Challenge 🎯 Problem: Merge Two Sorted Lists 🧩 Platform: LeetCode (#21) 💡 Category: Linked List 🧠 Problem Statement Given the heads of two sorted linked lists, merge them into a single sorted linked list and return its head. Example: Input: List1 = 1 → 2 → 4 List2 = 1 → 3 → 4 Output: 1 → 1 → 2 → 3 → 4 → 4 💡 Intuition This problem is all about two-pointer merging, similar to how you merge arrays in merge sort. We use a dummy node to simplify pointer handling. Then, at each step: Compare list1.val and list2.val Attach the smaller one to the merged list Move that list’s pointer forward Finally, append the remaining nodes. ⚙️ Complexity ⏱ Time: O(m + n) 💾 Space: O(1) ✨ Key Takeaway A clean example of how a dummy node simplifies linked list merging. This logic forms the foundation for many advanced problems like “Merge K Sorted Lists” and “Sort Linked List.” ✅ Day 20 of #Blind75 completed! Next up → Linked List Cycle Detection 🔁 📚 Full solutions available at my LeetCode profile: 👉 https://lnkd.in/gGEwKNFV #Blind75 #LeetCode #Java #LinkedList #DSA #CodingChallenge #ProblemSolving #CareerGrowth #100DaysOfCode #TechJourney
To view or add a comment, sign in
-
-
🌟 Day 56 of My #LeetCode Challenge – Word Pattern (#290) Today’s problem focused on validating if a string follows a specific character pattern — a great exercise in understanding mapping relationships and string manipulation. The task was to ensure that: 1️⃣ Each character in the pattern maps to exactly one word in the string. 2️⃣ No two characters map to the same word — maintaining a perfect one-to-one (bijection) relationship. To solve this, I used a HashMap<Character, String> to store the mappings between each character and its corresponding word. If a character already exists, I checked whether it points to the same word. If not, I returned false immediately. I also used containsValue() to make sure no word was reused for a different character. This problem reinforced the importance of careful logic flow, map lookups, and validation in both directions to avoid conflicts in mapping. 💡 Key learning: Building bijections with HashMaps is a common pattern in many string and mapping problems, and mastering it really improves problem-solving skills. #LeetCode #Day56 #Java #CodingChallenge #ProblemSolving #100DaysOfCode #DataStructures #HashMap #LearningEveryday
To view or add a comment, sign in
-
More from this author
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