Longest Common Substring

Longest Common Substring

GeeksforGeeks

Step-by-Step Solution Approach: Longest Common Substring

Finding the longest common substring between two strings is a classic problem in dynamic programming. Let's break down the solution step by step:

Problem Statement:

Given two strings str1 and str2, find the length of the longest common substring. A substring is a contiguous sequence of characters within a string.


Approach: Dynamic Programming

  1. Initialize a 2D DP Table:

  • Create a 2D table dp where dp[i][j] will store the length of the longest common substring ending at str1[i-1] and str2[j-1].
  • The dimensions of the dp table will be (len(str1) + 1) x (len(str2) + 1).

  1. Filling the DP Table:

  • If the characters str1[i-1] and str2[j-1] match, then the value of dp[i][j] will be dp[i-1][j-1] + 1.
  • If they don't match, set dp[i][j] to 0 because the common substring breaks.

  1. Tracking the Maximum Length:

  • Keep track of the maximum value in the dp table. This value will be the length of the longest common substring.

  1. Return the Result:

  • After filling the table, the maximum value found in the table is the length of the longest common substring.


Java Implementation:


CODE
STREAK 493

Explanation of the Code:

  • Initialization: We first initialize the dp table and a variable maxLength to track the length of the longest common substring.
  • Nested Loops: We use two nested loops to iterate over every character pair in str1 and str2.
  • Character Matching: If the characters match, we update dp[i][j] to be dp[i-1][j-1] + 1, meaning we extend the previous common substring by one character.
  • Result: Finally, we return the maximum value found in the dp table.

Time Complexity:

  • The time complexity of this solution is O(n * m), where n is the length of str1 and m is the length of str2.

Space Complexity:

  • The auxiliary space complexity is also O(n * m) due to the dp table.


Conclusion:

This dynamic programming approach efficiently finds the length of the longest common substring between two strings. It's a classic example of how dynamic programming can be applied to solve problems involving substrings and subsequences.


GITHUB:

SMILE_KHAN-JAVA+PYTHON


#Java #DynamicProgramming #ProblemSolving #LongestCommonSubstring #CodeOfTheDay #ProgrammingTips #LinkedInLearning

要查看或添加评论,请登录

Pathan Ismailkhan的更多文章

  • Array Subset

    Array Subset

    ?? Problem: You're given two arrays: a1[] and a2[], where both may contain duplicates, and the goal is to determine…

  • Intersection Point in Linked Lists

    Intersection Point in Linked Lists

    When working with linked lists, finding where two lists intersect can be tricky especially when efficiency is crucial!…

  • Is Linked List Length Even?

    Is Linked List Length Even?

    To solve the problem of determining if the length of a linked list is even or odd, let's consider an efficient approach…

  • Count Linked List Nodes

    Count Linked List Nodes

    ?? Problem: Today’s challenge is a fundamental one in data structures—finding the length of a Singly Linked List. ??…

  • Two Smallests in Every Subarray

    Two Smallests in Every Subarray

    ? Short Summary: In today's CODE OF THE DAY, we tackle how to find the maximum sum of the two smallest elements in…

  • Reorganize The Array

    Reorganize The Array

    ?? Summary: In today’s "Code of the Day," we explore an exciting problem: rearranging an array so that arr[i] = i. ??…

  • Max distance between same elements

    Max distance between same elements

    ?? Summary: In today’s "Code of the Day," we tackle a classic problem: finding the maximum distance between repeated…

    3 条评论
  • Largest Pair Sum

    Largest Pair Sum

    To solve the problem of finding the largest pair sum in an array of distinct integers, we can utilize a simple and…

  • Not a subset sum

    Not a subset sum

    ????? Problem: Given a sorted array of positive integers, find the smallest positive integer that cannot be represented…

  • 2491. Divide Players Into Teams of Equal Skill

    2491. Divide Players Into Teams of Equal Skill

    ????? Problem: You are given an even-length array of players' skills and the goal is to divide them into teams of 2…

社区洞察

其他会员也浏览了