159. Longest Substring with At Most Two Distinct Characters

时间:2020-05-30
本文章向大家介绍159. Longest Substring with At Most Two Distinct Characters,主要包括159. Longest Substring with At Most Two Distinct Characters使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
package LeetCode_159

/**
 * 159. Longest Substring with At Most Two Distinct Characters
 * (Locked by leetcode)
 * https://www.lintcode.com/problem/longest-substring-with-at-most-two-distinct-characters/description
 *
 * Given a string s , find the length of the longest substring t that contains at most 2 distinct characters.

Example 1:
Input: "eceba"
Output: 3
Explanation: t is "ece" which its length is 3.

Example 2:
Input: "ccaabbb"
Output: 5
Explanation: t is "aabbb" which its length is 5.
 * */
class Solution {
    /*
   Sliding Window solution. Time complexity:O(n), Space complexity:O(256);
    * 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 lengthOfLongestSubstringTwoDistinct(s: String): 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()]++
            while (counter > 2) {
                occMap[s[i].toInt()]--
                if (occMap[s[i].toInt()] == 0) {
                    counter--
                }
                i++
            }
            bestLength = Math.max(bestLength, j - i + 1)
            j++
        }
        //print(bestLength)
        return bestLength
    }
}

原文地址:https://www.cnblogs.com/johnnyzhao/p/12992129.html