Day 44 of 365 Days of code 😵💫 Qn 1) Decode String approach: stack took me a while to solve, but the problem was worth the time taken. 1)keep on pushing the characters into the stack until you encounter a " ] " closing bracket. 2) if a closing bracket is encountered initialize an empty string and keep popping the elements and add it to a string until a open square bracket is found on top of the stack 3) If the top element in the stack is an integer, multiply the integer with the string, and append it to the string 4) elif the top element is a string add the string to the top element 5) else append the string 6) reverse the stack, pop the reversed stack and add it to string res 7) return res gn #365daysOfCode #NeetCode #leetcode #DSA #python #LeetCode #ProblemSolving #Algorithms #365dayschallenge
Decoding Strings with Stack Approach
More Relevant Posts
-
🔥 Day 98 — #100DaysOfLeetCode ✅ Problem: Special Binary String (Hard) Today’s problem focused on special binary strings — strings where: • Number of 1s = number of 0s • Every prefix has ≥ as many 1s as 0s 🎯 Goal: Find the lexicographically largest string obtainable by swapping adjacent special substrings. 🧠 Approach: Use a counter to split the string into valid special segments. Recursively process inner substrings. Sort segments in descending order. Join them back together. ⚡ Example Input: "11011000" Output: "11100100" ⏱ Complexity Time: O(n log n) Space: O(n) 💡 Key Insight: Each valid segment behaves like a balanced block → reorder blocks for maximum lexicographic value. Hard problem ✔️ Confidence +1 🚀 #LeetCode #DSA #CodingJourney #HardProblems #Python
To view or add a comment, sign in
-
-
🚀 Day 54 of #100DaysOfCode 🧩 Problem: Minimum Number of Flips to Make the Binary String Alternating Today I solved an interesting problem involving string manipulation, rotations, and sliding window techniques. 💡 Key Idea: An alternating binary string can only follow two patterns: • "010101..." • "101010..." Since the problem allows rotating the string (moving the first character to the end), the trick is to consider all rotations efficiently. 🔑 Optimization Trick: Instead of rotating the string repeatedly, we can concatenate the string with itself ("s + s") and use a sliding window of size "n" to simulate all rotations. Then we compare each window with both alternating patterns and count the minimum flips required. ⚡ Concepts Used • Sliding Window • Greedy Thinking • String Pattern Matching • Optimization Trick ("s + s" rotation) 💻 Language: Python This problem was a great exercise in thinking about string rotations and pattern mismatches efficiently in O(n) time. 📈 Consistency is the key to improving problem-solving skills! #LeetCode #CodingChallenge #Python #DataStructures #Algorithms #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
The Basics of Strings Focus: String Properties & Indexing Day 3 was all about Strings. I thought I knew what a "word" was, but Python sees things a bit differently. 🧐 What clicked today: Case Sensitivity: Python is picky! Variable and variable are two different citizens. Slicing: String Methods: Indexing: Learning that counting starts at 0 was a bit of a brain-bender at first, but it makes so much sense now. Immutability: This was the big "Aha!" moment—knowing that once a string is created, you can’t just reach in and change a character. You have to build a new one. It’s these small details that make the difference between code that runs and code that crashes. 💻 #Strings #CodingJourney #PythonLearning #ContinuousGrowth #PythonDay3
To view or add a comment, sign in
-
-
Day 68 of 365 Days of code Qn 1) Min stack Approach: use an extra stack(to store minimum element up to the particular index) for push: push the value into the stack, check if the val is lesser than the element at the top of minstack, if yes, push the value, else push the copy of the top element in minstack for pop: pop from both of the stacks for returnmin: return minstack[top] :) good night #365daysOfCode #NeetCode #leetcode #DSA #python #LeetCode #ProblemSolving #Algorithms #365dayschallenge
To view or add a comment, sign in
-
-
LeetCode Problem 83: Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. The below implementation in Python successfully resolves this in time complexity of O(n) where n is length of the list with constant space complexity. The approach is simple, handle the base case first like list having 0 or 1 number of nodes and then write the logic of handling lists of length greater than one. Maintain two pointers, one to keep track of present node and other to keep track of previous node. Check if the value of both nodes match or not, if match update the pointing of previous node, point its next to the present.next. #LeetCode #LinkedList #Python #CompetitiveProgramming #Algorithms #DataStructures #ProblemSolving
To view or add a comment, sign in
-
-
Merge Intervals (LeetCode 56) - Medium Key Learnings: Sorting: By sorting intervals based on their start times, we can process them in a single pass (O(N)). Overlap Logic: Comparing the current_start with the previous_end time is the secret sauce to identifying overlaps. Space-Time Tradeoff: Sorting takes O(N log N) time, and we use O(N) space to store the results. #CodingJourney #LeetCode #Blind75 #SDEPreparation #SoftwareEngineering #Python #DataStructures
To view or add a comment, sign in
-
-
Day 67 of 365 Days of code the infamous brainrot number Qn 1) Reorder list You are given the head of a singly linked-list. The positions of a linked list of length = 7 for example, can intially be represented as: [0, 1, 2, 3, 4, 5, 6] Reorder the nodes of the linked list to be in the following order: [0, 6, 1, 5, 2, 4, 3] Notice that in the general case for a list of length = n the nodes are reordered to be in the following order: [0, n-1, 1, n-2, 2, n-3, ...] You may not modify the values in the list's nodes, but instead you must reorder the nodes themselves. approach: use 5 pointers(scary :")) #365daysOfCode #NeetCode #leetcode #DSA #python #LeetCode #ProblemSolving #Algorithms #365dayschallenge
To view or add a comment, sign in
-
-
🚀 Day 44 of #100DaysOfCode 📌 Problem: 762 – Prime Number of Set Bits in Binary Representation Today I solved an interesting bit manipulation problem on LeetCode that combines binary representation with prime number logic. 🔎 Problem Summary: Given two integers left and right, count how many numbers in that range have a prime number of set bits (1s) in their binary form. 💡 Key Insight: The maximum number of set bits for numbers ≤ 10⁶ is 20. So we only need to check prime numbers up to 20: {2, 3, 5, 7, 11, 13, 17, 19} For each number, count the set bits using: Python Copy code num.bit_count() ✅ Python Solution: Python Copy code class Solution: def countPrimeSetBits(self, left: int, right: int) -> int: primes = {2, 3, 5, 7, 11, 13, 17, 19} count = 0 for num in range(left, right + 1): if num.bit_count() in primes: count += 1 return count ⏱ Time Complexity: O(n) 🧠 Concepts Used: Bit Manipulation | Prime Numbers | Set Every day I’m getting more comfortable with binary operations and optimization techniques. #LeetCode #Day42 #CodingJourney #Python #ProblemSolving #BitManipulation #100DaysOfCode
To view or add a comment, sign in
-
-
LeetCode 20 | Valid Parentheses 🔥 🔹 Data Structure: Stack 🔹 Concept: LIFO matching for bracket validation 🔹 Time Complexity: O(n) 🔹 Space Complexity: O(n) Push opening brackets, match closing brackets carefully 💡 Stack problems strengthen logical flow control and pattern recognition. #LeetCode #DSA #Stack #Python #CodingPractice #ProblemSolving
To view or add a comment, sign in
-
LeetCode Problem 1545: "Given two positive integers n and k, the binary string Sn is formed as follows: S1 = "0" Si = Si - 1 + "1" + reverse(invert(Si - 1)) for i > 1 Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0)." Approach: Initialize an array with a single element '0' which serves as the string1 then iteratively calculate the consecutive strings and store them in the array. Finally, form the final string using the join() function and return the kth character of the string. #Python #LeetCode #String #ProblemSolving #CompetitiveProgramming #DataStructures #Algorithm
To view or add a comment, sign in
-
More from this author
Explore related topics
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