You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.1 KiB
Java
35 lines
1.1 KiB
Java
import java.util.*;
|
|
|
|
/**
|
|
* Template code to help you parse the standard input
|
|
* according to the problem statement.
|
|
**/
|
|
class Solution {
|
|
|
|
public static void main( String[] args ) {
|
|
// hint: read values via Scanner methods
|
|
var scanner = new Scanner( System.in );
|
|
|
|
// code your solution here
|
|
Map<String, Integer> lastOccurrences = new HashMap<>();
|
|
|
|
int startIndex = 0;
|
|
int maxLength = 0;
|
|
int index = 0;
|
|
|
|
while ( scanner.hasNext() ) {
|
|
Integer previousOccurrence = lastOccurrences.put( scanner.next(), index++ );
|
|
if ( previousOccurrence != null ) {
|
|
if ( previousOccurrence >= startIndex ) {
|
|
maxLength = Math.max( maxLength, index - 1 - startIndex );
|
|
startIndex = previousOccurrence + 1;
|
|
}
|
|
}
|
|
}
|
|
maxLength = Math.max( maxLength, index - startIndex );
|
|
|
|
// Write result with System.out.println()
|
|
System.out.println( maxLength );
|
|
}
|
|
}
|