█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 4915

Problema 0x59 - Substring más larga con máximo K caracteres diferentes

  • Autor: meow
  • Fecha: 2021-09-15 02:49:37 UTC (Hace más de 3 años)
Caso # Resultado Tiempo Memoria
#1
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#2
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#3
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#4
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#5
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#6
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#7
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#8
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#9
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#10
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#11
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#12
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#13
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#14
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#15
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#16
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#17
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#18
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#19
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#20
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#21
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#22
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#23
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
#24
Error de compilación
                      Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:6: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
              ^
  symbol:   class Map
  location: class Main
Main.java:13: error: cannot find symbol
        final Map<Character, Integer> freqMap = new HashMap<>();
                                                    ^
  symbol:   class HashMap
  location: class Main
4 errors

                    
Puntos totales: 0 / 100

Código

 public class Main {
    public static void main(String[] args) {
        // Substring más larga con máximo K caracteres diferentes
        // "codeo"
        // 3
        Scanner scanner = new Scanner(System.in);
        final String input = scanner.next();
        final int k = scanner.nextInt();
        
        //Solution here boy
        int result = 0;
        int i = 0;
        final Map<Character, Integer> freqMap = new HashMap<>();
        
        for(int j = 0; j < input.length(); j++) {
            char rightChar = input.charAt(j);
            freqMap.put(rightChar, freqMap.getOrDefault(rightChar, 0) + 1);
            
            while(freqMap.size() > k) {
                char leftChar = input.charAt(i);
                freqMap.put(leftChar, freqMap.getOrDefault(leftChar, 0) - 1);
                if(freqMap.get(leftChar) == 0) {
                    freqMap.remove(leftChar);
                }
                i++;
            }
            
            result = Math.max(result, j - i + 1);
        }
        
        System.out.println(result);
        scanner.close();
    }
}