#200DaysOfCode – Day 110 Palindrome Partitioning Task: Given a string s, partition it such that every substring is a palindrome, and return all possible valid partitions. Example: Input: s = "aab" Output: [["a","a","b"], ["aa","b"]] My Approach: Used Backtracking to explore all possible partitions. At each step, checked whether the current substring is a palindrome. If valid, added it to the path and continued recursively. Backtracked to explore other possibilities. Time Complexity: Exponential (due to all possible partitions) Space Complexity: O(N) (recursion stack) Backtracking problems may look complex at first, but breaking them into choices, constraints, and recursion makes them much more manageable. #takeUforward #200DaysOfCode #Java #ProblemSolving #LeetCode #Backtracking #Recursion #DSA #CodingJourney #CodeNewbie
Palindrome Partitioning with Backtracking
More Relevant Posts
-
📝 Day 11/30 – LeetCode #26 (Remove Duplicates from Sorted Array) | Java A brute-force approach using extra space would work, but the constraint here is to modify the array in-place. Since the array is already sorted, duplicates always appear next to each other. Using a two-pointer approach, one pointer tracks the position of the last unique element while the other scans through the array. Whenever a new value is found, it is placed at the next valid position. This results in an O(n) solution with O(1) extra space. This problem reinforced how understanding input constraints can significantly simplify the solution. #LeetCode #Java #DSA #TwoPointers #Arrays #ProblemSolving #Consistency #LearningInPublic
To view or add a comment, sign in
-
-
Java Stream API: Pipeline Power! → Intermediate (lazy: map, filter, sorted…) → Terminal (eager: collect, reduce…) List<Integer> evensSquared = Arrays.asList(1,2,3,4,5).stream() .filter(n -> n%2==0) .map(n -> n*n) .collect(Collectors.toList()); // [4, 16] Transform collections declaratively. #Java #Collections #Streams #Java8 #Map #FunctionalProgramming #CleanCode #List #Arrays #Coding #StreamAPI #Arrays #Intermediate #TerminalOperations #Collectors
To view or add a comment, sign in
-
-
Hello Everyone, Day 15 / #100DaysOfCode LeetCode 2943 — Maximize Area of Square Hole in Grid (Medium) Problem: You are given a grid formed by horizontal and vertical bars. Some bars can be removed. After removing any number of bars, you must form the largest possible square-shaped hole and return its area. The challenge is to determine how many consecutive bars can be removed horizontally and vertically to create the maximum square. Approach: Observation + Sorting - A square hole is formed by removing consecutive bars - The side of the square depends on: - Maximum consecutive removable horizontal bars - Maximum consecutive removable vertical bars - The side length is the minimum of the two Steps: - Sort hBars and vBars - Find the longest streak of consecutive numbers in each array - Add +1 because bars define lines, and the gap is between them - Take side = min(maxHGap, maxVGap) - Return side × side Why this works: The largest square is bounded by the smallest dimension where continuous space exists. Extra space in one direction cannot compensate for lack in the other. Complexity: - Time: O(n log n) due to sorting - Space: O(1) extra space (ignoring sort) #Leetcode #DSA #Java
To view or add a comment, sign in
-
-
Hello Everyone, Day 13 / #100DaysOfCode LeetCode 3453 — Separate Squares I (Medium) Problem: Given multiple axis-aligned squares on a 2D plane, find the minimum y-coordinate of a horizontal line such that the total area above the line equals the total area below it. Overlapping areas are counted multiple times. Approach: - Observed that the area difference (above − below) changes monotonically as the line moves up - Applied binary search on the y-coordinate - For each candidate height, computed how much area of every square lies below the line and compared it with the total area Key insight: Once the monotonic behavior is identified, the problem reduces to a clean binary search on the answer space. #LeetCode #DSA #Java
To view or add a comment, sign in
-
-
LeetCode POTD #1292 - Maximum Side Length of a Square with Sum <= Threshold Solved this one the right way, no brute force, no guesswork. Used 2D prefix sums to compute submatrix sums in O(1), then applied binary search on the square size to keep the overall complexity tight. Approach in short: Build a prefix sum matrix to query any square’s sum instantly Binary search on possible side lengths Validate each candidate square efficiently Track the maximum valid side length This keeps the logic clean and the time complexity under control, even for larger grids. Problems like these are a good reminder that knowing when to combine techniques (prefix sums + binary search) matters more than just knowing them individually. On to the next one. #LeetCode #POTD #DSA #Java #ProblemSolving
To view or add a comment, sign in
-
-
How Parallel reduce() Actually Works (Under the Hood) 1. The stream is partitioned into multiple sub-streams (e.g., one thread gets [2,3], another [4,5,6]) 2. Each sub-stream computes its partial result using the accumulator → Thread 1: 0 + 2 + 3 = 5 → Thread 2: 0 + 4 + 5 + 6 = 15 3. The partial results are then combined using the same accumulator → 5 + 15=20 List<Integer> numbers = Arrays.asList(2, 3, 4, 5, 6) int sum = numbers.parallelStream() .reduce(0, (a, b) -> a + b); // Still 20 #Java #StreamAPI #FunctionalProgramming #JavaTips #SoftwareDevelopment #Coding #Streams #ParallelSrreams
To view or add a comment, sign in
-
-
🌟 Day 25/30 – Remove Nth Node From End of Linked List Approach: Use a dummy node to simplify edge cases (like removing the head). Move the head pointer n steps ahead. Move both pointers together until head reaches null. Delete the target node by skipping it (dummy.next = dummy.next.next). Complexity: Time: O(n) Space: O(1) Key Takeaway: Two-pointer technique with a dummy node makes linked list deletions clean and avoids special-case handling. #25Day #DSA #LinkedList #TwoPointers #LeetCode #Java #ProblemSolving #CodingJourney #30DayChallenge
To view or add a comment, sign in
-
-
🌟 Day 29/30 – Add Two Numbers Approach: Instead of handling multiple loops for different list lengths, I used one single loop. At each step, I: Added values from both lists (if present) Included the carry Created a new node with sum % 10 Updated carry using sum / 10 Missing nodes are treated as 0, which simplifies the logic a lot. Complexity: Time: O(max(n, m)) Space: O(max(n, m)) Key Takeaway: A dummy node + single loop can replace multiple edge-case loops and make the solution much cleaner. #DSA #LeetCode #Java #LinkedList #ProblemSolving #Day29 #30DayChallenge #Coding
To view or add a comment, sign in
-
-
🔹 Day 98 – LeetCode Practice 📌 Problem: Path Sum II (LeetCode #113) 📊 Difficulty: Medium 🧠 Problem Overview: Given the root of a binary tree and a target sum, the task is to find all root-to-leaf paths where the sum of node values equals the target. Each valid path should be returned as a list of values. ✅ Approach Used: Traversed the tree using depth-first search. Maintained the current path and cumulative sum while moving down the tree. When a leaf node was reached, checked if the sum matched the target. Used backtracking to explore all possible paths correctly. 📈 Submission Results: Status: Accepted ✅ Runtime: 2 ms Memory Usage: 45.54 MB 💡 Reflection: This problem is a great example of how recursion and backtracking work together in tree problems. Managing state carefully is key to collecting all valid paths without duplication. #LeetCode #BinaryTree #DFS #Backtracking #Java #DSA #CodingPractice
To view or add a comment, sign in
-
-
📅 Day 68 of #100DaysOfLeetCode 🧩 Problem: 1312. Minimum Insertion Steps to Make a String Palindrome 💪 Difficulty: Hard 🧠 Key Insight To make a string a palindrome using minimum insertions, we should preserve the longest palindromic subsequence (LPS) and insert characters around it. 💡 Important Observation: The Longest Palindromic Subsequence (LPS) of a string = Longest Common Subsequence (LCS) between the string and its reverse 📌 Formula: Minimum Insertions = n − LPS ⏱️ Complexity Time: O(n²) Space: O(n²) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
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