File size: 3,081 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) Dr. Dirk Lellinger. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;

namespace SlobViewer.Collections.Text
{
  /// <summary>
  /// Determination of the longest common substring of two strings.
  /// </summary>
  public class LongestCommonSubstringOfTwoStrings
  {
    private int[][] _lcsArray = new int[0][];
    private int _allocatedLengthFirstString;
    private int _allocatedLengthSecondString;

    /// <summary>
    /// Determines the length of the longest common substring of two strings.
    /// </summary>
    /// <param name="firstString">The first string.</param>
    /// <param name="secondString">The second string.</param>
    /// <returns>The length of the longest common substring of the two strings.</returns>
    public int LongestCommonSubstring(string firstString, string secondString)
    {
      int firstLength = firstString.Length;
      int secondLength = secondString.Length;
      Reallocate(firstLength + 1, secondLength + 1);
      int result = 0;
      for (int i = 0; i <= firstLength; i++)
      {
        for (int j = 0; j <= secondLength; j++)
        {
          if (i == 0 || j == 0)
          {
            if (j < secondLength)
              _lcsArray[i][j] = 0;
          }
          else if (firstString[i - 1] == secondString[j - 1])
          {
            _lcsArray[i][j] = _lcsArray[i - 1][j - 1] + 1;

            result = Math.Max(result, _lcsArray[i][j]);
          }
          else
          {
            _lcsArray[i][j] = 0;
          }
        }
      }
      return result;
    }

    /// <summary>
    /// Pre-allocates helper space in order to avoid multiple allocations when a instance of this class is used repeatedly.
    /// </summary>
    /// <param name="maxLengthOfFirstString">The maximum possible length of the first string.</param>
    /// <param name="maxLengthOfSecondString">The maximum possible length of the second string.</param>
    public void Preallocate(int maxLengthOfFirstString, int maxLengthOfSecondString)
    {
      Reallocate(maxLengthOfFirstString + 1, maxLengthOfSecondString + 1);
    }


    private void Reallocate(int lengthFirstString, int lengthSecondString)
    {
      if (lengthFirstString > _allocatedLengthFirstString)
      {
        int[][] oldArr = _lcsArray;

        _allocatedLengthFirstString = lengthFirstString;
        _lcsArray = new int[_allocatedLengthFirstString][];
        Array.Copy(oldArr, _lcsArray, oldArr.Length);
        if (lengthSecondString <= _allocatedLengthSecondString)
        {
          for (int i = _lcsArray.Length + 1; i < lengthFirstString; ++i)
          {
            _lcsArray[i] = new int[_allocatedLengthSecondString];
          }
        }
      }

      if (lengthSecondString > _allocatedLengthSecondString)
      {
        _allocatedLengthSecondString = lengthSecondString;

        for (int i = 0; i < _lcsArray.Length; ++i)
        {
          _lcsArray[i] = new int[_allocatedLengthSecondString];
        }
      }
    }
  }
}