Pokemon (100 Marks)
Sergei B. was very pleased, and now he wants to visit as few flats as possible in order to collect Pokemons of all types that appear in this house. Your task is to help him and determine this minimum number of flats he has to visit Sergei B., the young coach of Pokemons, has found the big house which consists of n flats ordered in a row from left to right. It is possible to enter each flat from the street. It is possible to go out from each flat. Also, each flat is connected with the flat to the left and the flat to the right. Flat number 1 is only connected with the flat number 2 and the flat number n is only connected with the flat number n - 1.
There is exactly one Pokemon of some type in each of these flats. Sergei B. asked residents of the house to let him enter their flats in order to catch Pokemons. After consulting the residents of the house decided to let Sergei B. enter one flat from the street, visit several flats and then go out from some flat. But they won't let him visit the same flat more than once.
Input Format
You will be given a function with string array as a single argument which contains the row s with the length n, it consists of uppercase and lowercase letters of English alphabet, the i-th letter equals the type of Pokemon, which is in the flat number i.
Constraints
1 < |S| < 10^5
Output Format
Return the minimum number of flats which Sergei B. should visit in order to catch Pokemons of all types which there are in the house.
Sample TestCase 1
Input
3
A
a
A
Output
2
- Solution:
import java.io.*;
import java.util.*;
public class CandidateCode {
public static void Count(String arr[], int n){
Map<String, Integer> map = new HashMap<>();
for(int i=0; i<n; i++){
if(map.containsKey(arr[i])){
map.put(arr[i], map.get(arr[i])+1);
}
else
map.put(arr[i],i);
}
System.out.print(map.size());
}
public static void main(String args[] ) throws Exception {
Scanner sc = new Scanner(System.in);
int len=sc.nextInt();
String arr[] = new String[len];
for(int i=0; i<arr.length; i++)
arr[i]=sc.next();
Count(arr, len);
}
}
No comments:
Post a Comment