NO.159 至多包含两个不同字符的最长子串

给定一个字符串 s ,找出 至多 包含两个不同字符的最长子串 t 。

示例 1:

输入: "eceba"
输出: 3
解释: t 是 "ece",长度为3。

示例 2:

输入: "ccaabbb"
输出: 5
解释: t 是 "aabbb",长度为5。



int lengthOfLongestSubstringTwoDistinct(char * s){
    int len=strlen(s);
    if(len<=0)return 0;
    int **state=(int **)malloc(sizeof(int *)*2);
    state[0]=(int *)malloc(sizeof(int )*len);//
    state[1]=(int *)malloc(sizeof(int )*len);
    int max=1;
    state[0][0]=1;
    state[1][0]=1;
    char tmp[2]={0};
    tmp[0]=s[0];
    for(int i=1;istate[1][i]?max:state[1][i];
    }
    free(state[0]);
    free(state[1]);
    free(state);
    return max;
}

执行用时 :8 ms, 在所有 C 提交中击败了100.00% 的用户

内存消耗 :7.6 MB, 在所有 C 提交中击败了100.00%的用户

你可能感兴趣的:(C/C++)