zoukankan      html  css  js  c++  java
  • 340. Longest Substring with At Most K Distinct Characters

    package LeetCode_340
    
    /**
     * 340. Longest Substring with At Most K Distinct Characters
     *  (Locked by leetcode)
     *  https://www.lintcode.com/problem/longest-substring-with-at-most-k-distinct-characters/description
     *
     *  Given a string S, find the length of the longest substring T that contains at most k distinct characters.
    
    Example 1:
    Input: S = "eceba" and k = 3
    Output: 4
    Explanation: T = "eceb"
    
    Example 2:
    Input: S = "WORLD" and k = 4
    Output: 4
    Explanation: T = "WORL" or "ORLD"
     * */
    class Solution {
        /*
        Sliding Window solution, Time complexity:O(n), Space complexity:O(256)
            e  c  e  b  a
               ^
               i
    
                     ^
                     j
    
        * occMap: e:1, c:1, b:1
        * counter: 3 (when counter > K, need to move i)
        * best length: 3
        *
        * when moving i:
        * occMap[s[i]]--
        * if (occMap[s[i]]==0){
        *   counter--
        * }
        *
        * when moving j:
        * if (occMap[s[j]])==0{
        *   counter++
        * }
        * occMap[s[j]]++
        *
        * bestLength = max(bestLength, length of current window)=>max(bestLength, j-i+1)
        *
        * counter: how many distinct character we have
        *
        * valid window condition:
        * window contains <= 2 distinct characters = window desirable;
        * when move i: when the window becomes not desirable (counter>2)
        * when move j: as long as counter<=2 (as long as window desirable)
        *
        * Sliding Window Tech important point:
        * 1.when to move i, and what have to do when moving i;
        * 2.when to move j, and what have to do when moving j;
        * 3.when to update the goal;
        * */
        fun lengthOfLongestSubstringKDistinct(s: String, k: Int): Int {
            val occMap = IntArray(256)
            var i = 0
            var j = 0
            var counter = 0
            var bestLength = 0
            while (j < s.length) {
                if (occMap[s[j].toInt()] == 0) {
                    counter++
                }
                occMap[s[j].toInt()]++
                //if the count of current distinct character lager than k,
                //decrease the left pointer and keep tracking the distinct character
                while (counter > k) {
                    occMap[s[i].toInt()]--
                    if (occMap[s[i].toInt()] == 0) {
                        counter--
                    }
                    i++
                }
                bestLength = Math.max(bestLength, j - i + 1)
                j++
            }
            //print(bestLength)
            return bestLength
        }
    }
  • 相关阅读:
    实现MAXIMO7.5工作流任务箱任务颜色提示功能
    MAXIMO 快速查找实现
    DELPHI 通过方法名执行方法
    MAXIMO收件箱中,检修路线修改为其它名称
    在Linux 上手工创建 oracle 11g R2 数据库
    解决 maximo7.X 设备树子节点显示不全
    C++转换构造函数和隐式转换函数
    类或者结构体用无参构造函数创建对象时不需要带括号, 否则会当成函数声明
    今天我注册自己的博客啦,吼吼吼。。
    css3学习
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13413053.html
Copyright © 2011-2022 走看看