Dynamic Programming: Basics and a Practical Example

Introduction: Dynamic programming is a problem-solving technique that can seem quite intimidating at first glance. However, once you grasp its fundamentals and see it in action, you'll find it to be a powerful tool in your coding arsenal. In this blog post, we'll demystify the basics of dynamic programming and walk through a practical example. We'll provide you with clear explanations and a complete Python program, so you can understand and apply this essential concept with confidence. Understanding Dynamic Programming: At its core, dynamic programming is all about breaking down a complex problem into smaller, overlapping subproblems. It's a technique used to efficiently solve problems by storing and reusing solutions to these subproblems, rather than recomputing them. This leads to significant improvements in runtime complexity. Example: Fibonacci Sequence with Dynamic Programming Let's start by tackling a classic problem: calculating Fibonacci numbers. The Fibonac...

Longest Palindromic Substring | leetcode

 Question Source

- Given a string s, find the longest palindromic substring in s

Example 1:

Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.

Example 2:

Input: s = "cbbd"
Output: "bb"

 


Solution: JAVA
class Solution {
    public String longestPalindrome(String s) {
        if(s.length() == 0) {
            return s;
        }
        
        int startIndex = 0;
        int endIndex = 0;
        int maxLength = 0;
        
        for(int i = 0; i < s.length(); i++) {
            
            int length = 1;
            int j = i - 1, k = i +1;
            
            while(j>=0 && k < s.length() && s.charAt(j) == s.charAt(k)) {
                length += 2;
                j--;
                k++;
            }
            
            if(maxLength < length) {
                maxLength = length;
                startIndex = j+1;
                endIndex = k-1;
            }
            
            length = 0;
            j = i - 1;
            k = i;
            
            while(j>=0 && k < s.length() && s.charAt(j) == s.charAt(k)) {
                length += 2;
                j--;
                k++;
            }
            
            if(maxLength < length) {
                maxLength = length;
                startIndex = j+1;
                endIndex = k-1;
            }
            
            length = 0;
            j = i;
            k = i + 1;
            
            while(j>=0 && k < s.length() && s.charAt(j) == s.charAt(k)) {
                length += 2;
                j--;
                k++;
            }
            
            if(maxLength < length) {
                maxLength = length;
                startIndex = j+1;
                endIndex = k-1;
            }
        }
        
        return s.substring(startIndex, endIndex+1);
    }

}



Time Complexity: O(n^2)

Comments

Popular posts from this blog

Array Transformation | biweekly-contest-12 | leetcode | Java Solution

Dynamic Programming: Basics and a Practical Example

leetcode | Longest Substring Without Repeating Characters Solution