title: How to find a common suffix in two strings?
body: <p>I'm trying to implement something that finds the common suffix between a number of strings, for the sake of illustrations, consider the following:</p>
  
  <pre>
  "The quick brown fox"
  "The not so quick brown fox"
  "The smelly brown fox"
  "The vicious brown fox"
  </pre>
  
  <p>To a human, it's hopefully obvious that the common suffix here is <code>" brown fox"</code>, and my naive implementation currently takes the first pair of strings, converts them both to char arrays, and then iterates over these until a character is found to be different, I then create a new string from this, and crop it to the length, reverse it back to the correct order, and return that. I then repeat using the result from the first string with the next string in the list.</p>
  
  <p>Whilst this is loosely O(N), performance of this isn't as good as I'd like, and I wondered before I spend a long time buried in the profiler if I'd missed a quicker way to do this within the .NET framework?</p>
  
  <p><strong>EDIT:</strong>
  Taking out the double reverses (which then means we don't need to convert to char arrays) gives pretty good performance, for the record, my implementation looks a little like:</p>
  
  <pre><code>    private string GetCommonSuffix(string[] lines)
      {
          int lineCount = lines.GetLength(0);
  
          string currentSuffix = lines[0];
          int currentSuffixLength = currentSuffix.Length;
          for (int i = 1; i &lt; lineCount; i++)
          {
              string thisLine = lines[i];
              if (!thisLine.EndsWith(currentSuffix))
              {
                  int thisLineLength = thisLine.Length;
                  int maxPossible = thisLineLength &lt; currentSuffixLength ? thisLineLength : currentSuffixLength;
  
                  if (maxPossible == 0)
                  {
                      return string.Empty;
                  }
  
                  for (int j = 1; j &lt; maxPossible; j++)
                  {
                      if( currentSuffix[ currentSuffixLength - j ] != thisLine[ thisLineLength - j ] )
                      {
                          currentSuffix = currentSuffix.Substring(currentSuffixLength - j + 1, j - 1);
                          currentSuffixLength = j - 1;
                          break;
                      }
                  }
              }
          }
  
          return currentSuffix;
      }
  </code></pre>