Skip to content

Latest commit

 

History

History

182-longest-path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Longest Path

Challenge Description:

You are given a 2D N×N matrix. Each element of the matrix is a letter: from ‘a’ to ‘z’. Your task is to find the length L of the longest path in which not a single letter is repeated. The path can start at any cell, the transfer to the next element can be vertical or horizontal.

Example of a 5×5 matrix, where L=15:

Figure 1

Input sample:

The first argument is a file with test cases. Each line contains a serialized N×N matrix .

For example:

qttiwkajeerhdgpikkeaaabwl
vavprkykiloeizzt
skwajgaaxqpfcxmadpwaraksnkbgcaukbgli
kaja
bjzanjikh

Output sample:

Print to stdout the length of the longest path of unique elements for each test case, one per line.

For example:

15
11
16
3
7

Constraints:

  1. N is in a range from 2 to 6.
  2. The number of test cases is 20.