Cody

# Problem 2361. Find the longest match inside two strings

Solution 2105887

Submitted on 26 Jan 2020
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
first = 'minimum'; second = 'maximum'; match = substrmatch( first, second ); assert( strcmp( match, 'imum' ) )

b = 'mi' c = [] b = 'min' c = [] b = 'mini' c = [] b = 'minim' c = [] b = 'minimu' c = [] b = 'minimum' c = [] b = 'in' c = [] b = 'ini' c = [] b = 'inim' c = [] b = 'inimu' c = [] b = 'inimum' c = [] b = 'ni' c = [] b = 'nim' c = [] b = 'nimu' c = [] b = 'nimum' c = [] b = 'im' c = 4 m = 2 str = 'im' b = 'imu' c = 4 m = 3 str = 'imu' b = 'imum' c = 4 m = 4 str = 'imum' b = 'mu' c = 5 b = 'mum' c = 5 b = 'um' c = 6

2   Pass
first = 'aabbcc'; second = 'abc'; match = substrmatch( first, second ); assert( strcmp( match, 'ab' ) || strcmp( match, 'bc' ) )

b = 'aa' c = [] b = 'aab' c = [] b = 'aabb' c = [] b = 'aabbc' c = [] b = 'aabbcc' c = [] b = 'ab' c = 1 m = 2 str = 'ab' b = 'abb' c = [] b = 'abbc' c = [] b = 'abbcc' c = [] b = 'bb' c = [] b = 'bbc' c = [] b = 'bbcc' c = [] b = 'bc' c = 2 b = 'bcc' c = [] b = 'cc' c = []

3   Pass
first = 'MATLAB'; second = 'LAPACK'; match = substrmatch( first, second ); assert( strcmp( match, 'LA' ) )

b = 'MA' c = [] b = 'MAT' c = [] b = 'MATL' c = [] b = 'MATLA' c = [] b = 'MATLAB' c = [] b = 'AT' c = [] b = 'ATL' c = [] b = 'ATLA' c = [] b = 'ATLAB' c = [] b = 'TL' c = [] b = 'TLA' c = [] b = 'TLAB' c = [] b = 'LA' c = 1 m = 2 str = 'LA' b = 'LAB' c = [] b = 'AB' c = []

4   Fail
first = 'abc'; second = 'def'; match = substrmatch( first, second ); assert( strcmp( match, '' ) )

b = 'ab' c = [] b = 'abc' c = [] b = 'bc' c = []

Assertion failed.

5   Pass
first = 'banana'; second = 'anagram'; match = substrmatch( first, second ); assert( strcmp( match, 'ana' ) )

b = 'ba' c = [] b = 'ban' c = [] b = 'bana' c = [] b = 'banan' c = [] b = 'banana' c = [] b = 'an' c = 1 m = 2 str = 'an' b = 'ana' c = 1 m = 3 str = 'ana' b = 'anan' c = [] b = 'anana' c = [] b = 'na' c = 2 b = 'nan' c = [] b = 'nana' c = [] b = 'an' c = 1 b = 'ana' c = 1 b = 'na' c = 2

6   Pass
first = 'string within a string'; second = 'within'; match = substrmatch( first, second ); assert( strcmp( match, 'within' ) )

b = 'st' c = [] b = 'str' c = [] b = 'stri' c = [] b = 'strin' c = [] b = 'string' c = [] b = 'string ' c = [] b = 'string w' c = [] b = 'string wi' c = [] b = 'string wit' c = [] b = 'string with' c = [] b = 'string withi' c = [] b = 'string within' c = [] b = 'string within ' c = [] b = 'string within a' c = [] b = 'string within a ' c = [] b = 'string within a s' c = [] b = 'string within a st' c = [] b = 'string within a str' c = [] b = 'string within a stri' c = [] b = 'string within a strin' c = [] b = 'string within a string' c = [] b = 'tr' c = [] b = 'tri' c = [] b = 'trin' c = [] b = 'tring' c = [] b = 'tring ' c = [] b = 'tring w' c = [] b = 'tring wi' c = [] b = 'tring wit' c = [] b = 'tring with' c = [] b = 'tring withi' c = [] b = 'tring within' c = [] b = 'tring within ' c = [] b = 'tring within a' c = [] b = 'tring within a ' c = [] b = 'tring within a s' c = [] b = 'tring within a st' c = [] b = 'tring within a str' c = [] b = 'tring within a stri' c = [] b = 'tring within a strin' c = [] b = 'tring within a string' c = [] b = 'ri' c = [] b = 'rin' c = [] b = 'ring' c = [] b = 'ring ' c = [] b = 'ring w' c = [] b = 'ring wi' c = [] b = 'ring wit' c = [] b = 'ring with' c = [] b = 'ring withi' c = [] b = 'ring within' c = [] b = 'ring within ' c = [] b = 'ring within a' c = [] b = 'ring within a ' c = [] b = 'ring within a s' c = [] b = 'ring within a st' c = [] b = 'ring within a str' c = [] b = 'ring within a stri' c = [] b = 'ring within a strin' c = [] b = 'ring within a string' c = [] b = 'in' c = 5 m = 2 str = 'in' b = 'ing' c = [] b = 'ing ' c = [] b = 'ing w' c = [] b = 'ing wi' c = [] b = 'ing wit' c = [] b = 'ing with' c = [] b = 'ing withi' c = [] b = 'ing within' c = [] b = 'ing within ' c = [] b = 'ing within a' c = [] b = 'ing within a ' c = [] b = 'ing within a s' c = [] b = 'ing within a st' c = [] b = 'ing within a str' c = [] b = 'ing within a stri' c = [] b = 'ing within a strin' c = [] b = 'ing within a string' c = [] b = 'ng' c = [] b = 'ng ' c = [] b = 'ng w' c = [] b = 'ng wi' c = [] b = 'ng wit' c = [] b = 'ng with' c = [] b = 'ng withi' c = [] b = 'ng within' c = [] b = 'ng within ' c = [] b = 'ng within a' c = [] b = 'ng within a ' c = [] b = 'ng within a s' c = [] b = 'ng within a st' c = [] b = 'ng within a str' c = [] b = 'ng within a stri' c = [] b = 'ng within a strin' c = [] b = 'ng within a string' c = [] b = 'g ' c = [] b = 'g w' c = [] b = 'g wi' c = [] b = 'g wit' c = [] b = 'g with' c = [] b = 'g withi' c = [] b = 'g within' c = [] b = 'g within ' c = [] b = 'g within a' c = [] b = 'g within a ' c = [] b = 'g within a s' c = [] b = 'g within a st' c = [] b = 'g within a str' c = [] b = 'g within a stri' c = [] b = 'g within a strin' c = [] b = 'g within a string' c = [] b = ' w' c = [] b = ' wi' c = [] b = ' wit' c = [] b = ' with' c = [] b = ' withi' c = [] b = ' within' c = [] b = ' within ' c = [] b = ' within a' c = [] b = ' within a ' c = [] b = ' within a s' c = [] b = ' within a st' c = [] b = ' within a str' c = [] b = ' within a stri' c = [] b = ' within a strin' c = [] b = ' within a string' c = [] b = 'wi' c = 1 b = 'wit' c = 1 m = 3 str = 'wit' b = 'with' c = 1 m = 4 str = 'with' b = 'withi' c = 1 m = 5 str = 'withi' b = 'within' c = 1 m = 6 str = 'within' b = 'within ' c = [] b = 'within a' c = [] b = 'within a ' c = [] b = 'within a s' c = [] b = 'within a st' c = [] b = 'within a str' c = [] b = 'within a stri' c = [] b = 'within a strin' c = [] b = 'within a string' c = [] b = 'it' c = 2 b = 'ith' c = 2 b = 'ithi' c = 2 b = 'ithin' c = 2 b = 'ithin ' c = [] b = 'ithin a' c = [] b = 'ithin a ' c = [] b = 'ithin a s' c = [] b = 'ithin a st' c = [] b = 'ithin a str' c = [] b = 'ithin a stri' c = [] b = 'ithin a strin' c = [] b = 'ithin a string' c = [] b = 'th' c = 3 b = 'thi' c = 3 b = 'thin' c = 3 b = 'thin ' c = [] b = 'thin a' c = [] b = 'thin a ' c = [] b = 'thin a s' c = [] b = 'thin a st' c = [] b = 'thin a str' c = [] b = 'thin a stri' c = [] b = 'thin a strin' c = [] b = 'thin a string' c = [] b = 'hi' c = 4 b = 'hin' c = 4 b = 'hin ' c = [] b = 'hin a' c = [] b = 'hin a ' c = [] b = 'hin a s' c = [] b = 'hin a st' c = [] b = 'hin a str' c = [] b = 'hin a stri' c = [] b = 'hin a strin' c = [] b = 'hin a string' c = [] b = 'in' c = 5 b = 'in ' c = [] b = 'in a' c = [] b = 'in a ' c = [] b = 'in a s' c = [] b = 'in a st' c = [] b = 'in a str' c = [] b = 'in a stri' c = [] b = 'in a strin' c = [] b = 'in a string' c = [] b = 'n ' c = [] b = 'n a' c = [] b = 'n a ' c = [] b = 'n a s' c = [] b = 'n a st' c = [] b = 'n a str' c = [] b = 'n a stri' c = [] b = 'n a strin' c = [] b = 'n a string' c = [] b = ' a' c = [] b = ' a ' c = [] b = ' a s' c = [] b = ' a st' c = [] b = ' a str' c = [] b = ' a stri' c = [] b = ' a strin' c = [] b = ' a string' c = [] b = 'a ' c = [] b = 'a s' c = [] b = 'a st' c = [] b = 'a str' c = [] b = 'a stri' c = [] b = 'a strin' c = [] b = 'a string' c = [] b = ' s' c = [] b = ' st' c = [] b = ' str' c = [] b = ' stri' c = [] b = ' strin' c = [] b = ' string' c = [] b = 'st' c = [] b = 'str' c = [] b = 'stri' c = [] b = 'strin' c = [] b = 'string' c = [] b = 'tr' c = [] b = 'tri' c = [] b = 'trin' c = [] b = 'tring' c = [] b = 'ri' c = [] b = 'rin' c = [] b = 'ring' c = [] b = 'in' c = 5 b = 'ing' c = [] b = 'ng' c = []

7   Pass
first = 'yes and no'; second = 'ankles and noses'; match = substrmatch( first, second ); assert( strcmp( match, 'es and no' ) )

b = 'ye' c = [] b = 'yes' c = [] b = 'yes ' c = [] b = 'yes a' c = [] b = 'yes an' c = [] b = 'yes and' c = [] b = 'yes and ' c = [] b = 'yes and n' c = [] b = 'yes and no' c = [] b = 'es' c = 5 15 m = 2 str = 'es' b = 'es ' c = 5 m = 3 str = 'es ' b = 'es a' c = 5 m = 4 str = 'es a' b = 'es an' c = 5 m = 5 str = 'es an' b = 'es and' c = 5 m = 6 str = 'es and' b = 'es and ' c = 5 m = 7 str = 'es and ' b = 'es and n' c = 5 m = 8 str = 'es and n' b = 'es and no' c = 5 m = 9 str = 'es and no' b = 's ' c = 6 b = 's a' c = 6 b = 's an' c = 6 b = 's and' c = 6 b = 's and ' c = 6 b = 's and n' c = 6 b = 's and no' c = 6 b = ' a' c = 7 b = ' an' c = 7 b = ' and' c = 7 b = ' and ' c = 7 b = ' and n' c = 7 b = ' and no' c = 7 b = 'an' c = 1 8 b = 'and' c = 8 b = 'and ' c = 8 b = 'and n' c = 8 b = 'and no' c = 8 b = 'nd' c = 9 b = 'nd ' c = 9 b = 'nd n' c = 9 b = 'nd no' c = 9 b = 'd ' c = 10 b = 'd n' c = 10 b = 'd no' c = 10 b = ' n' c = 11 b = ' no' c = 11 b = 'no' c = 12

8   Pass
first = 'three apples'; second = 'one apple'; match = substrmatch( first, second ); assert( strcmp( match, 'e apple' ) )

b = 'th' c = [] b = 'thr' c = [] b = 'thre' c = [] b = 'three' c = [] b = 'three ' c = [] b = 'three a' c = [] b = 'three ap' c = [] b = 'three app' c = [] b = 'three appl' c = [] b = 'three apple' c = [] b = 'three apples' c = [] b = 'hr' c = [] b = 'hre' c = [] b = 'hree' c = [] b = 'hree ' c = [] b = 'hree a' c = [] b = 'hree ap' c = [] b = 'hree app' c = [] b = 'hree appl' c = [] b = 'hree apple' c = [] b = 'hree apples' c = [] b = 're' c = [] b = 'ree' c = [] b = 'ree ' c = [] b = 'ree a' c = [] b = 'ree ap' c = [] b = 'ree app' c = [] b = 'ree appl' c = [] b = 'ree apple' c = [] b = 'ree apples' c = [] b = 'ee' c = [] b = 'ee ' c = [] b = 'ee a' c = [] b = 'ee ap' c = [] b = 'ee app' c = [] b = 'ee appl' c = [] b = 'ee apple' c = [] b = 'ee apples' c = [] b = 'e ' c = 3 m = 2 str = 'e ' b = 'e a' c = 3 m = 3 str = 'e a' b = 'e ap' c = 3 m = 4 str = 'e ap' b = 'e app' c = 3 m = 5 str = 'e app' b = 'e appl' c = 3 m = 6 str = 'e appl' b = 'e apple' c = 3 m = 7 str = 'e apple' b = 'e apples' c = [] b = ' a' c = 4 b = ' ap' c = 4 b = ' app' c = 4 b = ' appl' c = 4 b = ' apple' c = 4 b = ' apples' c = [] b = 'ap' c = 5 b = 'app' c = 5 b = 'appl' c = 5 b = 'apple' c = 5 b = 'apples' c = [] b = 'pp' c = 6 b = 'ppl' c = 6 b = 'pple' c = 6 b = 'pples' c = [] b = 'pl' c = 7 b = 'ple' c = 7 b = 'ples' c = [] b = 'le' c = 8 b = 'les' c = [] b = 'es' c = []

9   Pass
first = 'hello there'; second = 'jello that'; match = substrmatch( first, second ); assert( strcmp( match, 'ello th' ) )

b = 'he' c = [] b = 'hel' c = [] b = 'hell' c = [] b = 'hello' c = [] b = 'hello ' c = [] b = 'hello t' c = [] b = 'hello th' c = [] b = 'hello the' c = [] b = 'hello ther' c = [] b = 'hello there' c = [] b = 'el' c = 2 m = 2 str = 'el' b = 'ell' c = 2 m = 3 str = 'ell' b = 'ello' c = 2 m = 4 str = 'ello' b = 'ello ' c = 2 m = 5 str = 'ello ' b = 'ello t' c = 2 m = 6 str = 'ello t' b = 'ello th' c = 2 m = 7 str = 'ello th' b = 'ello the' c = [] b = 'ello ther' c = [] b = 'ello there' c = [] b = 'll' c = 3 b = 'llo' c = 3 b = 'llo ' c = 3 b = 'llo t' c = 3 b = 'llo th' c = 3 b = 'llo the' c = [] b = 'llo ther' c = [] b = 'llo there' c = [] b = 'lo' c = 4 b = 'lo ' c = 4 b = 'lo t' c = 4 b = 'lo th' c = 4 b = 'lo the' c = [] b = 'lo ther' c = [] b = 'lo there' c = [] b = 'o ' c = 5 b = 'o t' c = 5 b = 'o th' c = 5 b = 'o the' c = [] b = 'o ther' c = [] b = 'o there' c = [] b = ' t' c = 6 b = ' th' c = 6 b = ' the' c = [] b = ' ther' c = [] b = ' there' c = [] b = 'th' c = 7 b = 'the' c = [] b = 'ther' c = [] b = 'there' c = [] b = 'he' c = [] b = 'her' c = [] b = 'here' c = [] b = 'er' c = [] b = 'ere' c = [] b = 're' c = []