Day 92/100 – #100DaysOfCode 🚀 | #Java #DynamicProgramming #Strings ✅ Problem Solved: Longest Palindromic Subsequence (LeetCode) 🧩 Problem Summary: Given a string, find the length of the longest subsequence that is also a palindrome. (A subsequence does not need to be contiguous.) 💡 Approach Used: ✔ Used Dynamic Programming (2D DP) ✔ Define dp[i][j] as the length of the longest palindromic subsequence in substring s[i…j] Logic: If s[i] == s[j] → dp[i][j] = 2 + dp[i+1][j-1] Else → dp[i][j] = max(dp[i+1][j], dp[i][j-1]) Fill the table bottom-up by increasing substring length ⚙ Time Complexity: O(N²) 📦 Space Complexity: O(N²) ✨ Takeaway: Problems involving subsequences often hint toward DP. Breaking the string into overlapping subproblems makes complex patterns manageable. #Java #LeetCode #DynamicProgramming #Strings #100DaysOfCode #CodingChallenge
Java Longest Palindromic Subsequence Solution with Dynamic Programming
More Relevant Posts
-
Day 97/100 – #100DaysOfCode 🚀 | #Java #Strings #Logic ✅ Problem Solved: Longest Uncommon Subsequence I (LeetCode) 🧩 Problem Summary: Given two strings, find the length of the longest uncommon subsequence — a subsequence that appears in one string but not in the other. 💡 Approach Used: ✔ Observational / logical approach ✔ If both strings are equal, no uncommon subsequence exists ✔ If they are different, the longer string itself is the answer Why it works: A string is always a subsequence of itself If strings differ, the longer one cannot be a subsequence of the shorter ⚙ Time Complexity: O(1) 📦 Space Complexity: O(1) ✨ Takeaway: Some problems look like DP but are solved with pure logic. Always check for simple observations before overengineering. #Java #LeetCode #Strings #Logic #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
Day 99/100 – #100DaysOfCode 🚀 | #Java #PrefixSum #HashMap ✅ Problem Solved: Continuous Subarray Sum (LeetCode 523) 🧩 Problem Summary: Given an integer array and an integer k, determine if the array has a continuous subarray of size at least 2 whose sum is a multiple of k. 💡 Approach Used: ✔ Used Prefix Sum + HashMap ✔ Key observation: If two prefix sums have the same remainder when divided by k, the subarray between them has a sum divisible by k. Steps: Maintain running sum Store (prefixSum % k) → earliest index in a HashMap If the same remainder appears again and the subarray length ≥ 2 → return true Initialize map with (0 → -1) to handle edge cases ⚙ Time Complexity: O(N) 📦 Space Complexity: O(K) (where K = number of unique remainders) ✨ Takeaway: Modular arithmetic paired with prefix sums is a powerful pattern for detecting divisible subarrays in linear time. #Java #LeetCode #PrefixSum #HashMap #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
🚀 LeetCode 1662 – Check If Two String Arrays are Equivalent Combine two string arrays using String.join("", array) and compare with .equals() — solves the problem in just one line! 💡 Returns false if any array is empty. #LeetCode #Java #DSA #ProblemSolving #CodingJourney #LearnToCode #SoftwareEngineer #DailyCoding
To view or add a comment, sign in
-
-
Day 94/100 – #100DaysOfCode 🚀 | #Java #DynamicProgramming #UnboundedKnapsack ✅ Problem Solved: Coin Change II (LeetCode 518) 🧩 Problem Summary: Given an integer amount and an array of coin denominations, return the number of combinations that make up the amount. Each coin can be used unlimited times, and the order of coins does not matter. 💡 Approach Used: ✔ Used Dynamic Programming (1D DP) ✔ This is a classic Unbounded Knapsack problem Steps: Initialize dp[0] = 1 (one way to make amount 0) For each coin: Iterate from coin to amount Update dp[i] += dp[i - coin] This ensures combinations are counted, not permutations ⚙ Time Complexity: O(N × amount) 📦 Space Complexity: O(amount) (where N = number of coins) ✨ Takeaway: The order of loops in DP matters! Iterating coins first avoids counting duplicate permutations and keeps combinations unique. #Java #LeetCode #DynamicProgramming #UnboundedKnapsack #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
#200DaysOfCode – Day 108 Combination Sum III Problem:- Combination Sum III Task:- Find all valid combinations of k numbers that sum up to n, using only numbers from 1 to 9, where: Each number is used at most once No duplicate combinations are allowed Example: Input: k = 3, n = 7 Output: [[1, 2, 4]] My Approach:- Used Backtracking (DFS) to explore all possible combinations. Started from a given number to avoid duplicates. Stopped recursion when: The combination size exceeded k The sum became negative Added the combination to the result only when: Exactly k numbers were chosen The sum became 0 Time Complexity:- Exponential (bounded due to range 1–9) Space Complexity:- O(k) (recursive stack + temporary list) Backtracking may look complex at first, but with clear base conditions and pruning, it becomes a powerful and elegant tool for solving combination problems efficiently. #takeUforward #200DaysOfCode #Java #ProblemSolving #LeetCode #Backtracking #Recursion #DSA #CodingJourney #CodeNewbie
To view or add a comment, sign in
-
-
This Java nested loop prints a step-based number pattern, helping me understand how small changes in loop conditions create different outputs. Each pattern strengthens: ✔ Logical thinking ✔ Control over nested loops ✔ Problem-solving approach Basics done right lead to long-term growth 💻🔥 👉 Consistency over perfection #Java #NestedLoops #PatternProgramming #ProgrammingLogic #JavaBasics #CodingJourney #LearnByDoing #DeveloperMindset
To view or add a comment, sign in
-
-
This Java nested loop prints an increasing number pattern, but more importantly, it strengthened my understanding of how logic grows step by step. 💡 Every pattern teaches something new: ✔ Control of nested loops ✔ Relationship between rows & columns ✔ Building problem-solving mindset Simple code today, stronger developer tomorrow 💻🔥 👉 Consistency > Complexity #Java #NestedLoops #PatternProgramming #LogicBuilding #JavaDeveloper #CodingJourney #LearnByDoing #ProgrammingBasics
To view or add a comment, sign in
-
-
LeetCode Practice - 896. Monotonic Array 🔷Logic Behind “Monotonic Array” (Java) ✔An array is called monotonic if it moves in only one direction: 📌Either it never decreases (increasing or equal) 📌Or it never increases (decreasing or equal) ✔So the idea is very simple: 📌Check if the array follows at least one of these directions. 🔍 Step 1 — Use two flags We take two boolean variables: boolean increasing = true; boolean decreasing = true; 🔄 Step 2 — Compare neighboring elements We loop through the array and compare every pair: What does this mean? If nums[i] > nums[i+1] → array is going down, so it cannot be increasing If nums[i] < nums[i+1] → array is going up, so it cannot be decreasing We keep eliminating possibilities. 🧪 Example 1 [1, 2, 2, 3] Comparisons: 1 ≤ 2 → ok for increasing 2 ≤ 2 → ok 2 ≤ 3 → ok No violation for increasing, so: increasing = true decreasing = false ➡ Result = true 🧪 Example 2 [1, 3, 2] Comparisons: 1 < 3 → not decreasing 3 > 2 → not increasing So: increasing = false decreasing = false ➡ Result = false ✅ Final decision ✔return increasing || decreasing; ✔If either increasing or decreasing is still true, the array is monotonic. #LeetCode #Java #CodingPractice #ProblemSolving #DSA #Array #DeveloperJourney #TechLearning
To view or add a comment, sign in
-
-
A clean visualization of how Java handles Arrays: Syntax: int[] arr = new int[5]; Structure: Contiguous memory locations. Access: Fast retrieval using index numbers. Simple, efficient, and essential for every Java developer. #Java #Programming #CodeNewbie #DeveloperResources
To view or add a comment, sign in
-
-
Day 29/100 ✅ LeetCode 138: Copy List with Random Pointer Solved this problem by creating a deep copy of a linked list where each node contains an additional random pointer. The approach ensures that both next and random pointers are copied correctly without modifying the original list. Key steps involved: Create copied nodes Link copied nodes with original nodes Assign random pointers efficiently Separate the original and cloned lists This problem helped me strengthen my understanding of linked list manipulation and pointer mapping. Solution:-https://lnkd.in/gD9TWWsg #LeetCode #LeetCode138 #CopyListWithRandomPointer #LinkedList #DSA #Java #ProblemSolving #CodingPractice #DeepCopy #DataStructures #Day29
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