Nested encoded strings can look complex at first, but stacks make the decoding process systematic and manageable. 🚀 Day 144/365 — DSA Challenge Solved: Decode String Problem idea: Given an encoded string in the format: k[encoded_string] Repeat the string inside brackets exactly k times and return the fully decoded result. Efficient approach: 1. Traverse the string character by character 2. Build numbers for repeat counts 3. Use stacks to store: - Previous strings - Repeat counts 4. When ] appears: - Repeat current substring - Merge it with previous stored string Key Learning: - Stack-based string decoding - Handling nested patterns - Managing multiple states simultaneously - Parsing strings efficiently ⏱ Time: O(n) 📦 Space: O(n) Day 144/365 complete. 💻 221 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #Stack #String #LeetCode #ProblemSolving #LearningInPublic
Muhammad Anas Qadri’s Post
More Relevant Posts
-
𝐃��𝐲 𝟏𝟑𝟎/𝟑𝟔𝟓 🚀 📌 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐎𝐓𝐃: 𝐋𝐨𝐧𝐠𝐞𝐬𝐭 𝐂𝐨𝐦𝐦𝐨𝐧 𝐒𝐮𝐟𝐟𝐢𝐱 𝐐𝐮𝐞𝐫𝐢𝐞𝐬 🔎 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: • Build a Trie using reversed words from wordsContainer • Each Trie node stores: - shortest word length - corresponding smallest index • Traverse query strings in reverse order • Follow longest matching suffix path in Trie • Return best matching index stored at deepest valid node 🔍 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦: • Trie + String Processing ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: • O(total characters in wordsContainer + wordsQuery) 🧠 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: • O(total characters in Trie) 📈 𝐊𝐞𝐲 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲: • Reversing strings transforms suffix problems into prefix matching problems 🔗 Problem Link: https://lnkd.in/dXYjF78v #LeetCode #LeetcodePOTD #365DaysOfCode #DSA #Java #Trie #Strings #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 53 of 200 💪 DSA Journey (Frequency Array + String) Today’s problem: Determine if Two Strings Are Close Solved using frequency counting and sorting approach. Key Idea: ✔️ Both strings must have same length ✔️ Both strings should contain same characters ✔️ Frequencies should match after sorting Approach: → Create two frequency arrays → Count frequency of each character → Check character existence → Sort both arrays → Compare both arrays Time Complexity: O(n) Space Complexity: O(1) Day 53 done. Keep going 🚀 #DSA #LeetCode #Java #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 13 — #100DaysOfLeetCode Today’s problem: Split Array Largest Sum (LeetCode 410) Initially, I tried solving this problem using brute force by checking every possible partition, but the complexity became too high. Then I explored DP Memoization, which was much better, but the most interesting part was learning the optimal approach using Binary Search + Greedy. ✔ Binary searched the answer space ✔ Greedily created partitions ✔ Minimized the largest subarray sum efficiently 📊 Complexity Comparison: Approach → Time → Space 🔸 Brute Force → O(2^n) → O(n) 🔸 DP Memoization → O(n² × k) → O(nk) ⭐ Binary Search + Greedy → O(n log(sum)) → O(1) Big takeaway from today: Sometimes the best solution comes from changing the way we search for the answer. #LeetCode #100DaysOfLeetCode #DSA #Java #ProblemSolving #Algorithms #CodingInterview #SoftwareDevelopment
To view or add a comment, sign in
-
-
#2553 — Separate the Digits in an Array (Easy) ✅ Problem: Given an array of positive integers, decompose each integer into its individual decimal digits and return them in a single array, preserving the original order of both numbers and digits. Example: nums = [13, 25, 83, 77] → [1, 3, 2, 5, 8, 3, 7, 7] Approach: Iterate through the input array, convert each integer to a string, extract every character, and map it back to its numeric value by subtracting the character '0'. Append the results sequentially. Implementation insight: The logic can be expressed in two equivalent styles — an explicit loop with an intermediate List, or a single Java stream pipeline using flatMap to flatten each number into its digit stream. Both achieve O(n × d) time and O(n × d) space, where d ≤ 6 (max digits per number). Key takeaways: String conversion keeps digit extraction clean and readable. The ordering guarantee (number order → digit order) is straightforward to maintain with a linear traversal. Stream pipelines can reduce boilerplate without sacrificing clarity when used judiciously. 🔗 Solution: https://lnkd.in/gG6cYw6Z #LeetCode #Java #Coding #Algorithms #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 54 / 100 | Find Minimum in Rotated Sorted Array Intuition: A rotated sorted array has two sorted parts. The minimum element is the pivot point where rotation happened. Using Binary Search: If nums[mid] > nums[end], minimum lies on the right side. Otherwise, minimum lies on the left side including mid. Approach: Initialize st = 0 and end = n - 1 Run binary search while st < end Find mid Compare nums[mid] with nums[end] If nums[mid] > nums[end] → search right half Else -> search left half including mid At the end, st points to the minimum element Complexity: Time Complexity: O(log n) Space Complexity: O(1) #100DaysOfCode #Java #DSA #LeetCode #BinarySearch #ProblemSolving
To view or add a comment, sign in
-
-
Grinding DSA (Day 97) Problem: 154 - Find Minimum in Rotated Sorted Array II ✅ Solved a tricky variation of the rotated sorted array problem where duplicates are also present. The main challenge was handling cases where binary search cannot directly decide the sorted half because of equal elements. Learned how reducing the search space carefully using mid and right pointer comparison still helps achieve an efficient solution in most cases. #LeetCode #DSA #BinarySearch #Arrays #ProblemSolving #CodingJourney #Java
To view or add a comment, sign in
-
-
Grinding DSA (Day 96) Problem: 153 - Find Minimum in Rotated Sorted Array ✅ Solved a classic binary search problem on rotated arrays. The challenge was to efficiently locate the minimum element without scanning the entire array. It was interesting to see how comparing mid and right pointers helps identify the unsorted half and narrow the search space in logarithmic time. #LeetCode #DSA #BinarySearch #Arrays #ProblemSolving #CodingJourney #Java
To view or add a comment, sign in
-
-
🎯day 22 🎯 Problem Name: Sqrt(x) 🚀 Problem Statement: Given a non-negative integer x, compute and return the square root of x. Only the integer part of the square root is needed (truncate the decimal). 💡 Solution Approach: 1️⃣ Binary Search: Start with l = 0 and r = x. Calculate mid = l + (r - l) / 2. If mid * mid > x, move to the left half (r = mid - 1). If mid * mid < x, move to the right half (l = mid + 1) and update res = mid. If mid * mid == x, return mid. 2️⃣ Return res after the loop ends. 📊 Time Complexity: O(logx) O(logx) 📊 Space Complexity: O(1) O(1) #CodingChallenge #BinarySearch #ProblemSolving #LeetCode #Java
To view or add a comment, sign in
-
-
Day 67 of Daily DSA 🚀 Solved LeetCode 145: Binary Tree Postorder Traversal ✅ Problem: Given the root of a binary tree, return the postorder traversal of its nodes' values. Problem Constraints: • Traverse nodes in Left → Right → Root order • Return traversal as a list • Handle empty tree cases Intuition: Postorder traversal ensures both subtrees are completely processed before visiting the current node. This naturally fits recursive DFS where left and right children are explored first. Approach: Used Recursion + DFS to perform postorder traversal. Understand: The recursive calls first visit the left subtree, then the right subtree, and finally add the current node value. This guarantees the required Left → Right → Root order. Steps: • Create an empty list for answer • Recursively visit left subtree • Recursively visit right subtree • Add current node value to list • Return final traversal list ⏱ Complexity: • Time: O(n) • Space: O(n) (recursion stack) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) • Memory: 43.26 MB A classic tree traversal problem — recursion makes it clean and elegant 🌳🔥 #DSA #LeetCode #Java #BinaryTree #Recursion #DFS #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 #DrGVishwanathanChallenge Day 39 Solved: Map Word Weights (Java) 🔤 💡 Key Takeaways: Calculated word weights using character frequency mapping Used ASCII indexing for efficient access Applied modulo arithmetic to map results into alphabet range ⚡ Core Idea: Convert characters → weights → cumulative sum → mapped character 🎯 Time Complexity: O(n × m) (n = number of words, m = average word length) 🎯 Space Complexity: O(1) 📌 Problems involving character mapping become much easier with ASCII arithmetic and array indexing techniques #DSA #Java #Strings #Arrays #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-