1. Diagonal on a Match, Else the Bigger Neighbour Optimal
Intuition
Trying every pick takes far too long, so build the answer up from the strings' beginnings. Make a table with a row for each letter of text1 and a column for each letter of text2: a cell holds the longest pick for text1 up to its row and text2 up to its column, and an extra row and column of 0s stand for an empty string, which shares nothing. Then compare the last letters, the row's and the column's. If they match, that letter can end the pick, so the cell is the diagonal, the cell up and to the left without either letter, plus 1. If they differ, the pick can't end on both, so drop one: the cell above has dropped the row's letter and the cell to the left the column's, and the cell keeps the bigger. Filling row by row, each row from left to right, the three cells a cell reads are always filled first, and the answer waits in the bottom-right cell, with both strings whole. Grabbing the first match you see is the trap: on "arc" and "care", matching care's c with arc's c leaves nothing of arc after it and stops at 1, and the table finds 2, "ar", by skipping that c. Matching last letters extend the diagonal; else keep the bigger neighbour.
Algorithm
1. Make a table with m + 1 rows and n + 1 columns, all starting at 0: row 0 and column 0 stand for an empty string.
2. Go through the rows from 1 to m, and each row's columns from 1 to n.
3. If the row's letter of text1 matches the column's letter of text2, the cell gets the diagonal, the cell up and to the left, plus 1.
4. Otherwise it gets the bigger of the cell above and the cell to its left.
5. Return the bottom-right cell.
Time & Space
Time O(m*n): each cell is filled once, with a single comparison. Space O(m*n): the table stores every cell. Two rows are enough, O(n): a cell only reads its own row and the row above it.