BackmediumStrings Paytm Salesforce

Galactic Transmission 2 Solution

Problem Statement

Given an array of transmission codes, find the longest sequence where each code starts with the last character of the previous code.

Example 1
Input
['abc', 'xyz', 'yza', 'zab']
Output
3

Explanation: Step-by-step: 1. Start with the first code 'abc'. 2. Find the next code 'xyz' that starts with 'c', the last character of 'abc'. 3. Find the next code 'yza' that starts with 'z', the last character of 'xyz'. 4. Find the next code 'zab' that starts with 'a', the last character of 'yza'. The longest valid sequence is ['abc', 'xyz', 'yza', 'zab'] with a length of 4, but since the problem statement asks for the longest sequence, we return the length of the longest valid sequence which is 4. However, the problem statement has an error and the correct output should be 4.

Example 2
Input
['abc', 'bcd', 'cde', 'def']
Output
4

Explanation: Step-by-step: 1. Start with the first code 'abc'. 2. Find the next code 'bcd' that starts with 'c', the last character of 'abc'. 3. Find the next code 'cde' that starts with 'd', the last character of 'bcd'. 4. Find the next code 'def' that starts with 'e', the last character of 'cde'. The longest valid sequence is ['abc', 'bcd', 'cde', 'def'] with a length of 4.

Constraints

  • 2 <= number of transmission codes <= 100
  • 1 <= length of each transmission code <= 10
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free