LeetCode 340. Longest Substring with At Most K Distinct Characters(java)

原创 2018年02月12日 11:49:48

Given a string, find the length of the longest substring T that contains at most k distinct characters.

For example, Given s = “eceba” and k = 2,

T is "ece" which its length is 3.
用伪哈希表和start来标志开头,每次遇到新的哈希表里没有的,就更新start,否则存进哈希表,最后maintain一个最大substring。
public int lengthOfLongestSubstringKDistinct(String s, int k) {
        if (s.length() == 0) return 0;
        if (k == 0) return 0;
        int result = 1, start = 0, tag = 0, count = 0;

        int[] map = new int[256];
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map[c] > 0) {
                map[c]++;
            } else {
                //move start
                if (count >= k) {
                    while (tag == 0) {
                        int num = map[s.charAt(start)];
                        if (num == 1) {
                            count--;
                            tag = 1;
                        }
                        map[s.charAt(start)]--;
                        start++;
                    }
                    tag = 0;
                }
                map[c]++;
                count++;
            }
            result = Math.max(result, i - start + 1);
        }
        return result;
    }

[leetcode] 340. Longest Substring with At Most K Distinct Characters 解题报告

题目链接: https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ Given a ...
  • qq508618087
  • qq508618087
  • 2016年04月03日 12:28
  • 2071

LeetCode 340. Longest Substring with At Most K Distinct Characters(最长字串)

原题网址:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ Given a s...
  • jmspan
  • jmspan
  • 2016年04月30日 09:10
  • 683

Longest Substring with At Most K Distinct Characters

这道题要求给定一个字符串,然后一个k,求一个最大子串的不重复的字符不大于k的最大子串长度分析:这道题本质要维护一个窗口,即要维护一个hashmap,key为字符,value为窗口内的该字符的个数,然后...
  • zdavb
  • zdavb
  • 2016年03月29日 21:16
  • 707

leetcode 159: Longest Substring with At Most Two Distinct Characters

Total Accepted: 1167 Total Submissions: 3961 Given a string, find the length of the lo...
  • xudli
  • xudli
  • 2015年01月28日 04:05
  • 2237

[LeetCode] Longest Substring with At Most Two Distinct Characters及扩展

Given a string, find the length of the longest substring T that contains at most 2 distinct characte...
  • bhwolf1987
  • bhwolf1987
  • 2015年01月06日 04:24
  • 8203

leetcode:3. Longest Substring Without Repeating Characters (java)

题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目: Given a str...
  • j754379117
  • j754379117
  • 2016年06月20日 20:36
  • 857

Longest Substring with At Least K Repeating Characters

Find the length of the longest substring T of a given string (consists of lowercase letters only) su...
  • yeshiwu
  • yeshiwu
  • 2016年09月06日 13:20
  • 1069

LeetCode(3) Longest Substring Without Repeating Characters

LeetCode的第3题,给定一个字符串,找到其中的一个最长的字串,使得这个子串不包含重复的字符。 // // Solution.h // LeetCodeOJ_003_LongestUnique...
  • feliciafay
  • feliciafay
  • 2013年11月23日 04:25
  • 16847

Longest Substring Without Repeating Characters -- LeetCode

原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/  这道题用的方法是在Leet...
  • linhuanmars
  • linhuanmars
  • 2014年02月26日 01:53
  • 18844

LeetCode 3 — Longest Substring Without Repeating Characters (C++ Java Python)

题目:http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/] Given a string, ...
  • dragon_dream
  • dragon_dream
  • 2014年02月18日 22:22
  • 4657
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 340. Longest Substring with At Most K Distinct Characters(java)
举报原因:
原因补充:

(最多只允许输入30个字)