🚀 Day 26 of my DSA Journey (LeetCode + Java) Today’s practice covered Stack, Two-Pointer string processing, and Prefix matching. Each problem had a different pattern, so it was a great mixed-learning day! ✅ Problems Solved Today: LC 20 – Valid Parentheses LC 443 – String Compression LC 14 – Longest Common Prefix 🧠 My Experience Solving These: 🔸 20. Valid Parentheses This is a classic stack-based validation problem. My steps: Push opening brackets into stack When closing bracket appears, check top of stack If matching pair → pop Else → return false Finally, if stack is empty → parentheses are valid. A very clean and logical stack problem. 🔸 443. String Compression This problem was interesting because it relies on two-pointer string traversal. Here’s how I solved it: Use one pointer to scan the string Count how many times a character repeats Write the character + its count into the main array Maintain a separate write index It’s an optimized in-place solution, and understanding the pointer movement is the key. 🔸 14. Longest Common Prefix A very popular string problem. My approach: Take the first string as the initial prefix Compare it with every next string Trim the prefix whenever mismatch occurs Continue until the shortest valid prefix remains Simple logic but very important technique for prefix-matching problems. 📌 Key Takeaway Today: Working on mixed patterns like Stack, String Compression, and Prefix Matching helps build versatility. ✔ Stack is perfect for matching pairs ✔ Two-pointer technique makes string processing efficient ✔ Prefix trimming is a powerful string strategy Every day I’m building stronger intuition toward string and stack problems. On to Day 27! 🚀 #DSA #LeetCode #Java #SlidingWindow #ProblemSolving #CodingJourney #Consistency #LearningDaily
Day 26 of DSA Journey: Stack, String Compression, and Prefix Matching
More Relevant Posts
-
🚀 Day 1 of #DSAChallenge 📌 LeetCode Daily Challenge 🧩 Problem: Minimize Maximum Pair Sum in an Array (1877) 💻 Language: Java 🔍 Problem Overview Given an array of even length, pair up the elements such that: ✅ Every element is used exactly once ✅ The maximum pair sum among all pairs is as small as possible 💡 Key Insight To minimize the maximum pair sum, we should: 👉 Pair the smallest number with the largest 👉 Pair the second smallest with the second largest, and so on This balancing strategy prevents any pair from becoming too large. 🛠 Approach Used 🔹 Sort the array 🔹 Use two pointers from both ends 🔹 Track the maximum sum among all pairs ⏱ Time Complexity: O(n log n) 📦 Space Complexity: O(1) (ignoring sorting space) ✅ Java Solution import java.util.Arrays; class Solution { public int minPairSum(int[] nums) { Arrays.sort(nums); int n = nums.length; int maxSum = 0; for(int i = 0; i < n/2; i++){ int pairSum = nums[i] + nums[n - 1 - i]; if(pairSum > maxSum){ maxSum = pairSum; } } return maxSum; } } 📚 Key Learnings ✔ Greedy strategies work well with proper intuition ✔ Sorting can simplify complex pairing problems ✔ Pairing extremes helps balance constraints #DSA #LeetCodeDaily #Java #GreedyAlgorithm #ProblemSolving #BackendDeveloper #Algorithm #DataStructures #CodingInterview
To view or add a comment, sign in
-
-
Journey Today I solved the Anagram String Problem in Java. An anagram means two strings contain the same characters with the same frequency, just arranged in a different order. Example: listen → silent ✅ triangle → integral ✅ 🔍 What I practiced today: String traversal Converting String to char[] using toCharArray() Using Arrays.sort() for comparison Understanding character frequency logic Improving problem-solving thinking 💡 One important learning: Small mistakes like case sensitivity (toCharArray() not tocharArray()) can break the entire program. Attention to detail matters! Consistency is teaching me more than talent ever could. Every day I understand Java a little deeper. 36 days completed. No breaks. Just progress. 💪 #Day36 #CodingJourney #Java #Anagram #ProblemSolving #100DaysOfCode #Consistency #Learning My code ===== //import java.util.Arrays; class Solution { public static boolean areAnagrams(String s1, String s2) { // code here char a[]=s1.toCharArray(); char b[]=s2.toCharArray(); Arrays.sort(a); Arrays.sort(b); if(a.length!=b.length) { return false; } for(int i=0;i<a.length;i++) { if(a[i]!=b[i]) { return false; } } return true; } }
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟗 – 𝐑𝐞𝐯𝐢𝐬𝐢𝐭𝐢𝐧𝐠 𝟐-𝐃 𝐀𝐫𝐫𝐚𝐲𝐬 𝐢𝐧 𝐉𝐚𝐯𝐚 One important realization: 👉 In Java, a 2-D array is NOT actually a “matrix”. It is an array of arrays. That means: Each row is a separate array object. Rows can have different lengths. Memory is not necessarily continuous like in C/C++ Example: int[][] arr = new int[3][]; arr[0] = new int[2]; arr[1] = new int[4]; arr[2] = new int[3]; This is called a 𝑱𝒂𝒈𝒈𝒆𝒅 𝑨𝒓𝒓𝒂𝒚. Many beginners assume: arr.length == total elements ❌ But actually: arr.length → number of rows arr[i].length → number of columns in that specific row This small understanding prevents many traversal bugs. The deeper you understand memory structure, the cleaner your logic becomes. 🔹 Beginner Level 2-D Array Problems : These are simple but build strong fundamentals. 1️⃣ Print a 2D array----> Traverse and print all elements. 2️⃣ Find the sum of all elements 3️⃣ Row-wise sum-----> Print sum of each row separately. 4️⃣ Column-wise sum---->This helps understand traversal direction change. 5️⃣ Find the largest element----> Simple scanning problem. #Java #DataStructures #LearningInPublic #dailyChallenge
To view or add a comment, sign in
-
Friends who know me know Java is my one true love ☕️❤️ And with Valentine’s Day coming, I’m deepening this love by learning one new thing about Java each day. Today’s topic: JITs, Interpreters, Bytecode and Native Code ⚙️ JVM Bytecode vs Native Code - 📦JVM bytecode → CPU-agnostic (same bytecode can run anywhere there’s a JVM) - 🔩Native code → CPU-specific instructions (differs across CPU/OS) ⚙️Why Bytecode Can Be Slower (initially) When running bytecode, the JVM may start by interpreting it: it reads instructions and decides what CPU-specific work to do. Native code doesn’t need that translation step — it just runs. ⚙️How the JVM Runs Bytecode (2 key modes) 1) 🧠Interpreter (fast to start, slower per execution) Executes bytecode instruction-by-instruction; While doing that, it also profiles the program, tracking: - which methods are called frequently - which loops run a lot - which branches are “hot” This profiling data becomes fuel for the next stage. 2)⚡JIT Compiler (fast after warm-up) - Compiles frequently executed (“hot”) bytecode into optimized native machine code - Optimizes based on real runtime behavior, not guesses - If behavior changes, the JVM can discard and re-optimize compiled code ⚙️Mental Picture - Startup: JVM is learning + rearranging furniture - Warm-up: JVM figures out what matters - Steady state: JVM stops experimenting and runs efficiently ⚙️What you’ll often observe in real systems 1. Performance improves after a short period 2. Higher CPU usage early on (interpreter + JIT compiling in the background) 3. Unpredictable latency during warm-up due to: - JIT compilation pauses - early / irregular GC cycles - class loading & initialization Takeaway: let the program reach steady state before benchmarking. ⚙️TL;DR 🧠Interpreter: “Use Google Translate for every sentence.” ⚡JIT: “After repeating the same phrases, you write a bilingual cheat sheet — now you don’t translate every time.” #Java #JVM #JIT #PerformanceEngineering #Backend #LearningInPublic
To view or add a comment, sign in
-
Every line of code tells a backend story 💻📖 As a learner, I try to understand what really happens behind the code I write. while learning Java, this simple example made me stop and think 👇 String s = "Hello"; s = s + " World"; At first, it feels like we’re just updating the same String. But that’s not what Java does. In Java, a String variable doesn’t store the text itself. It stores a reference to an object in memory. I started imagining memory like a lined notebook 📒. Because String is immutable, Java never edits the same line. Instead, it writes "Hello World" on a new line and moves the reference. ➡️ New line = new object Now compare that with: StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); Here, Java edits the same line. No new object, no extra memory. ➡️ Same line = same object This is why StringBuilder is preferred when we modify strings frequently. Learning this helped me understand not just what to write, but why Java behaves the way it does ⚙️ Tomorrow, I’ll try to explain Python variables using the same notebook analogy 🐍📘 If you’re also learning concepts step by step, follow me — I share simple explanations like this while learning 🚀 #Java #String #StringBuilder #BackendDevelopment #LearningInPublic #ProgrammingConcepts
To view or add a comment, sign in
-
-
🚨 𝗪𝗵𝘆 𝗱𝗼𝗲𝘀 𝗝𝗮𝘃𝗮 𝗰𝗼𝗱𝗲 𝗿𝘂𝗻 𝗲𝘃𝗲𝗻 𝘄𝗶𝘁𝗵 𝗮 “𝘄𝗿𝗼𝗻𝗴” 𝗺𝗮𝗶𝗻() 𝗺𝗲𝘁𝗵𝗼𝗱? Today I learned something interesting while practicing Java 👇 I wrote this code: 𝘤𝘭𝘢𝘴𝘴 𝘏𝘦𝘭𝘭𝘰𝘞𝘰𝘳𝘭𝘥 { 𝘱𝘶𝘣𝘭𝘪𝘤 𝘴𝘵𝘢𝘵𝘪𝘤 𝘷𝘰𝘪𝘥 𝘮𝘢𝘪𝘯() { 𝘚𝘺𝘴𝘵𝘦𝘮.𝘰𝘶𝘵.𝘱𝘳𝘪𝘯𝘵𝘭𝘯("𝘏𝘦𝘭𝘭𝘰 𝘸𝘰𝘳𝘭𝘥"); } } 👉 𝗔𝗰𝗰𝗼𝗿𝗱𝗶𝗻𝗴 𝘁𝗼 𝗝𝗮𝘃𝗮 𝘀𝘁𝗮𝗻𝗱𝗮𝗿𝗱𝘀, 𝘁𝗵𝗶𝘀 𝗶𝘀 𝗡𝗢𝗧 𝗮 𝘃𝗮𝗹𝗶𝗱 𝗲𝗻𝘁𝗿𝘆 𝗽𝗼𝗶𝗻𝘁. The JVM strictly requires: public static void main(String[] args) Yet surprisingly, the code ran successfully in VS Code / online compilers 🤯 ❓ 𝗪𝗵𝘆 𝗱𝗼𝗲𝘀 𝘁𝗵𝗶𝘀 𝗵𝗮𝗽𝗽𝗲𝗻? Some IDEs and online compilers (VS Code, OneCompiler, etc.) help developers by: Wrapping the code internally Using their own valid main(String[] args) Manually calling our main() method behind the scenes So the JVM never directly starts from our method. ⚠️ 𝗜𝗺𝗽𝗼𝗿𝘁𝗮𝗻𝘁 𝘁𝗮𝗸𝗲𝗮𝘄𝗮𝘆 This behavior: ❌ Will fail in exams ❌ Will fail in interviews ❌ Will fail in real JVM environments ✅ Always use this (safe everywhere): public static void main(String[] args) 💡 𝗟𝗲𝘀𝘀𝗼𝗻 𝗹𝗲𝗮𝗿𝗻𝗲𝗱: Don’t rely on IDE magic — always follow JVM specifications. #Java #CoreJava #Programming #Learning #InterviewPrep #CSStudent #JavaTips
To view or add a comment, sign in
-
-
📌 Garbage Collection isn’t just cleanup — it’s Performance engineering! 🗓️ Day 3/21 – Mastering Java 🚀 Topic: Garbage Collection & GC Algorithms ♻️ Java manages memory for us, yet knowing how it works under the hood is a valuable skill for backend developers. 🔹 What is Garbage Collection? Garbage Collection (GC) is the JVM process that: - Identifies objects that are no longer used. - Frees their memory automatically. - Prevents memory-related crashes. 📌 An object becomes eligible for GC when no active references point to it. 🔹 How Garbage Collection Works (Simple Flow) Step 1: JVM tracks object references. Step 2: Unused objects are marked. Step 3: Memory is reclaimed. Step 4: Heap is compacted for efficiency. 📌 GC runs automatically — developers don’t call it manually. 🔹 Why Java Uses Generations 🧠 Most objects are created, used briefly and then discarded. So Java divides memory into generations: - Young Generation → new objects. - Old Generation → long-living objects 📌 This makes GC faster and more efficient. 🔹 Popular GC Algorithms - G1 GC: Balanced & predictable (default in modern Java) - CMS: Low pause times (older approach) - Parallel GC: High throughput. - ZGC: Ultra-low latency for large heaps. 📌 GC choice depends on latency vs throughput needs. 💡 Top 3 Frequently asked Interview Questions (From today’s topic) 1: When does an object become eligible for GC? 2: Why is GC generational? 3: Difference between G1 and Parallel GC? 💬 Share your answers or questions in the comments — happy to discuss! #21DaysOfJava #GarbageCollection #JavaGC #GCAlgorithms #JVM #JavaMemory
To view or add a comment, sign in
-
🚀 LeetCode #46 — Permutations (Backtracking in Java) Today I solved the Permutations problem using a clean swap-based backtracking approach. This problem is a great way to strengthen recursion and understand how backtracking explores all possible configurations systematically. 🧠 Problem Summary Given an array of distinct integers, generate all possible permutations. 💡 Approach Instead of using extra space to track used elements, I applied in-place swapping: Fix one index at a time Swap it with every possible candidate Recurse for the next index Backtrack (swap back) to restore the original order This ensures: ✔ All permutations are explored ✔ No duplicates ✔ Efficient space usage ✅ Java Solution class Solution { List<List<Integer>> sol = new ArrayList<>(); public List<List<Integer>> permute(int[] nums) { generate(0, nums); return sol; } public void generate(int i, int[] nums) { if (i == nums.length) { sol.add(Arrays.stream(nums).boxed().toList()); return; } for (int j = i; j < nums.length; j++) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; generate(i + 1, nums); temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } } } 📈 Complexity Time: O(n × n!) Space: O(n) recursion depth 🔍 Key Learnings ✨ Backtracking = Choose → Explore → Unchoose ✨ Swapping helps avoid extra memory usage ✨ Restoring state is critical for correct recursion Problems like this really improve intuition for recursion trees and algorithmic thinking. #LeetCode #Java #Backtracking #Recursion #DataStructures #Algorithms #CodingPractice #ProblemSolving #LearningJourney
To view or add a comment, sign in
-
🚀 A tiny mistake. A big lesson in Java. Recently while debugging a DSA solution, I ran into this condition: while(nums1[p1] == res[i-1] && p1 < n1) Looks perfectly fine at first glance, right? But Java evaluates left to right. So what actually happens is: nums1[p1] == res[i-1] // evaluated FIRST And only after that: p1 < n1 If p1 == n1, the code tries to access nums1[n1] → 💥 ArrayIndexOutOfBoundsException The fix? Just change the order: while(p1 < n1 && nums1[p1] == res[i-1]) Now bounds are checked before access. Safe. Correct. Stable. 🧠 Real lesson here: This wasn’t about syntax. This wasn’t about logic. This was about execution order. Small details in code structure can break entire algorithms. 💡 Takeaways: • Code is not just about what you write • It’s about how the compiler reads it • Order of conditions matters • Evaluation order matters • Safety checks must always come first • Clean logic must also be safe logic This one bug reminded me that: Great code isn’t just correct — it’s defensively written. The difference between a good developer and a strong developer is often attention to tiny details like these. Because in real systems, small mistakes don’t fail small — they fail big. #Java #DSA #Debugging #ProblemSolving #CleanCode #ProgrammingLessons #SoftwareEngineering #LearningByDoing #DeveloperLife #GrowthMindset
To view or add a comment, sign in
-
Debugging in Action: Reverse String Prefix Challenge I recently tackled a deceptively simple Java problem: reverse the first k characters of a string s. Sounds easy, right? But my initial implementation returned a wrong answer — and that’s where the real learning began. What went wrong? - Misused loop conditions (k > 1 instead of i >= 0) - Incorrect indexing (i = k instead of i = k - 1) - Confused logic in appending the rest of the string Fixed it with clarity: `java for (int i = k - 1; i >= 0; i--) { sb.append(s.charAt(i)); } for (int i = k; i < s.length(); i++) { sb.append(s.charAt(i)); } ` Lesson: Error codes aren’t obstacles — they’re feedback. Every “Wrong Answer” is a chance to refine logic, rethink assumptions, and grow as a developer. If you’ve debugged something recently, share your fix! Let’s normalize learning through mistakes 💡 Java #Debugging #CodingJourney #SoftwareEngineering #LinkedInLearning #AkashLearns
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