当前位置:首页|资讯

GFG 45 Count number of substrings

作者:您是打尖儿还是住店呢发布时间:2024-09-30

Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. 


Example 1:

Input:

S = "aba", K = 2

Output:

3

Explanation:

The substrings are:

"ab", "ba" and "aba".


Example 2:


Input:

S = "abaaca", K = 1

Output:

7

Explanation:

The substrings are:

"a", "b", "a", "aa", "a", "c", "a".


Your Task:

You don't need to read input or print anything. Your task is to complete the

function substrCount() which takes the string S and an integer K as inputs and returns the number

of substrings having exactly K distinct characters.


Expected Time Complexity: O(|S|).

Expected Auxiliary Space: O(1).


Constraints:

1 ≤ |S| ≤ 10

1 ≤ K ≤ 26

----

跟昨天周赛的题目很像,就是遇到恰好的情况的时候,就用2个函数相减就可以的,因为直接双指针的时候,会存在左指针什么时候向右走的情况不能确定。



Copyright © 2024 aigcdaily.cn  北京智识时代科技有限公司  版权所有  京ICP备2023006237号-1