Longest common substring GudangMovies21 Rebahinxxi LK21

      In computer science, a longest common substring of two or more strings is a longest string that is a substring of all of them. There may be more than one longest common substring. Applications include data deduplication and plagiarism detection.


      Examples



      The picture shows two strings where the problem has multiple solutions. Although the substring occurrences always overlap, it is impossible to obtain a longer common substring by "uniting" them.
      The strings "ABABC", "BABCA" and "ABCBA" have only one longest common substring, viz. "ABC" of length 3. Other common substrings are "A", "AB", "B", "BA", "BC" and "C".

      ABABC
      |||
      BABCA
      |||
      ABCBA


      Problem definition


      Given two strings,



      S


      {\displaystyle S}

      of length



      m


      {\displaystyle m}

      and



      T


      {\displaystyle T}

      of length



      n


      {\displaystyle n}

      , find a longest string which is substring of both



      S


      {\displaystyle S}

      and



      T


      {\displaystyle T}

      .
      A generalization is the k-common substring problem. Given the set of strings



      S
      =
      {

      S

      1


      ,

      ,

      S

      K


      }


      {\displaystyle S=\{S_{1},\ldots ,S_{K}\}}

      , where




      |


      S

      i



      |

      =

      n

      i




      {\displaystyle |S_{i}|=n_{i}}

      and





      n

      i


      =
      N


      {\textstyle \sum n_{i}=N}

      . Find for each



      2

      k

      K


      {\displaystyle 2\leq k\leq K}

      , a longest string which occurs as substring of at least



      k


      {\displaystyle k}

      strings.


      Algorithms


      One can find the lengths and starting positions of the longest common substrings of



      S


      {\displaystyle S}

      and



      T


      {\displaystyle T}

      in



      Θ


      {\displaystyle \Theta }





      (
      n
      +
      m
      )


      {\displaystyle (n+m)}

      time with the help of a generalized suffix tree. A faster algorithm can be achieved in the word RAM model of computation if the size



      σ


      {\displaystyle \sigma }

      of the input alphabet is in




      2

      o

      (


      log

      (
      n
      +
      m
      )


      )





      {\displaystyle 2^{o\left({\sqrt {\log(n+m)}}\right)}}

      . In particular, this algorithm runs in



      O

      (

      (
      n
      +
      m
      )
      log

      σ

      /



      log

      (
      n
      +
      m
      )



      )



      {\textstyle O\left((n+m)\log \sigma /{\sqrt {\log(n+m)}}\right)}

      time using



      O

      (

      (
      n
      +
      m
      )
      log

      σ

      /

      log

      (
      n
      +
      m
      )

      )



      {\displaystyle O\left((n+m)\log \sigma /\log(n+m)\right)}

      space. Solving the problem by dynamic programming costs



      Θ
      (
      n
      m
      )


      {\displaystyle \Theta (nm)}

      . The solutions to the generalized problem take



      Θ
      (

      n

      1


      +

      +

      n

      K


      )


      {\displaystyle \Theta (n_{1}+\cdots +n_{K})}

      space and



      Θ
      (

      n

      1




      n

      K


      )


      {\displaystyle \Theta (n_{1}\cdots n_{K})}

      time with dynamic programming and take



      Θ
      (

      n

      1


      +

      +

      n

      K


      )


      {\displaystyle \Theta (n_{1}+\cdots +n_{K})}

      time with a generalized suffix tree.


      = Suffix tree

      =

      The longest common substrings of a set of strings can be found by building a generalized suffix tree for the strings, and then finding the deepest internal nodes which have leaf nodes from all the strings in the subtree below it. The figure on the right is the suffix tree for the strings "ABAB", "BABA" and "ABBA", padded with unique string terminators, to become "ABAB$0", "BABA$1" and "ABBA$2". The nodes representing "A", "B", "AB" and "BA" all have descendant leaves from all of the strings, numbered 0, 1 and 2.
      Building the suffix tree takes



      Θ
      (
      N
      )


      {\displaystyle \Theta (N)}

      time (if the size of the alphabet is constant). If the tree is traversed from the bottom up with a bit vector telling which strings are seen below each node, the k-common substring problem can be solved in



      Θ
      (
      N
      K
      )


      {\displaystyle \Theta (NK)}

      time. If the suffix tree is prepared for constant time lowest common ancestor retrieval, it can be solved in



      Θ
      (
      N
      )


      {\displaystyle \Theta (N)}

      time.


      = Dynamic programming

      =
      The following pseudocode finds the set of longest common substrings between two strings with dynamic programming:

      function LongestCommonSubstring(S[1..r], T[1..n])
      L := array(1..r, 1..n)
      z := 0
      ret := {}

      for i := 1..r
      for j := 1..n
      if S[i] = T[j]
      if i = 1 or j = 1
      L[i, j] := 1
      else
      L[i, j] := L[i − 1, j − 1] + 1
      if L[i, j] > z
      z := L[i, j]
      ret := {S[(i − z + 1)..i]}
      else if L[i, j] = z
      ret := ret ∪ {S[(i − z + 1)..i]}
      else
      L[i, j] := 0
      return ret

      This algorithm runs in



      O
      (
      n
      r
      )


      {\displaystyle O(nr)}

      time. The array L stores the length of the longest common suffix of the prefixes S[1..i] and T[1..j] which end at position i and j, respectively. The variable z is used to hold the length of the longest common substring found so far. The set ret is used to hold the set of strings which are of length z. The set ret can be saved efficiently by just storing the index i, which is the last character of the longest common substring (of size z) instead of S[(i-z+1)..i]. Thus all the longest common substrings would be, for each i in ret, S[(ret[i]-z)..(ret[i])].
      The following tricks can be used to reduce the memory usage of an implementation:

      Keep only the last and current row of the DP table to save memory (



      O
      (
      min
      (
      r
      ,
      n
      )
      )


      {\displaystyle O(\min(r,n))}

      instead of



      O
      (
      n
      r
      )


      {\displaystyle O(nr)}

      )
      The last and current row can be stored on the same 1D array by traversing the inner loop backwards
      Store only non-zero values in the rows. This can be done using hash-tables instead of arrays. This is useful for large alphabets.


      See also


      Longest palindromic substring
      n-gram, all the possible substrings of length n that are contained in a string


      References




      External links



      Dictionary of Algorithms and Data Structures: longest common substring
      Perl/XS implementation of the dynamic programming algorithm
      Perl/XS implementation of the suffix tree algorithm
      Dynamic programming implementations in various languages on wikibooks
      working AS3 implementation of the dynamic programming algorithm
      Suffix Tree based C implementation of Longest common substring for two strings

    Kata Kunci Pencarian:

    longest common substringlongest common substring leetcodelongest common substring problemlongest common substring solutionlongest common substring calculatorlongest common substring gfg practicelongest common substring recursivelongest common substring in an array of stringslongest common substring coding ninjaslongest common substring memoization
    Algorithm Repository

    Algorithm Repository

    Algorithm Repository

    Algorithm Repository

    Longest Common Substring Problem | Techie Delight

    Longest Common Substring Problem | Techie Delight

    Longest Common Substring - InterviewBit

    Longest Common Substring - InterviewBit

    Longest Common Substring - InterviewBit

    Longest Common Substring - InterviewBit

    Longest Common Substring - InterviewBit

    Longest Common Substring - InterviewBit

    Longest Common Substring - InterviewBit

    Longest Common Substring - InterviewBit

    Longest Common Substring (Java) – Program Creek

    Longest Common Substring (Java) – Program Creek

    Longest Common Substring - LeetCode Discuss

    Longest Common Substring - LeetCode Discuss

    Longest Common Substring - Scaler Topics

    Longest Common Substring - Scaler Topics

    Longest Common Substring - Scaler Topics

    Longest Common Substring - Scaler Topics

    Longest Common Substring - Scaler Topics

    Longest Common Substring - Scaler Topics

    Search Results

    longest common substring

    Daftar Isi

    Longest Common Substring - GeeksforGeeks

    Sep 30, 2024 · Given two strings ‘s1‘ and ‘s2‘, find the length of the longest common substring. Example: The longest common substring is “Geeks” and is of length 5. The longest common …

    Longest common substring - Wikipedia

    In computer science, a longest common substring of two or more strings is a longest string that is a substring of all of them. There may be more than one longest common substring. …

    Longest Common Substring | Practice - GeeksforGeeks

    Your task is to find the length of the longest common substring among the given strings. Examples: Output: 4. Explanation: The longest common substring is "CDGH" with a length of …

    Longest Common Substring - InterviewBit

    Jun 27, 2023 · Given two strings, the task is to find the longest common substring present in the given strings in the same order. The substring is a contiguous sequence of characters within a …

    Jul 15, 2022 · To print the longest common substring, we use a variable end. When len [i] [j] is calculated, it is compared with maxlen. If maxlen is less than len [i] [j], then end is updated to i …

    Longest Common Subsequence - LeetCode

    Longest Common Subsequence - Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0. A subsequence …

    Longest Common Substring - LeetCode Discuss

    int m = str1.length(); int n = str2.length(); int maxLen = 0; int[][] dp = new int[m+1][n+1]; for(int i = 0; i<=m; i++){ for(int j = 0; j<=n; j++){ if(i == 0 || j == 0) dp[i][j] = 0; else if(str1.charAt(i-1) == …

    Longest Common Substring - Naukri Code 360

    Mar 14, 2021 · You have to find the length of the longest common substring. A substring is a continuous segment of a string. For example, "bcd" is a substring of "abcd", while "acd" or …

    Dynamic Programming - Longest Common Substring

    What is the Longest Common Substring: The longest substring is a sequence that appears in the same order and is necessarily contiguous in both strings. Example: Output: Length of Longest …

    Longest Common Substring - Naukri Code 360

    Return an integer representing the length of the longest common substring. You don’t need to print anything. Just implement the given function. The longest common substring is “jkl”, of …