2024/10/30 灵茶题单 滑动窗口 最短/最小
This commit is contained in:
parent
caba747303
commit
127d23b479
|
@ -0,0 +1,30 @@
|
||||||
|
package com.cool.ling_cha_mount.sliding_windows;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @Author: Cool
|
||||||
|
* @Date: 2024/10/30/17:07
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
public class Num1358 {
|
||||||
|
public int numberOfSubstrings(String s) {
|
||||||
|
int res = 0;
|
||||||
|
int left = 0;
|
||||||
|
int[] arr = new int[3];
|
||||||
|
for (int i = 0; i < s.length(); i++) {
|
||||||
|
++arr[s.charAt(i) - 'a'];
|
||||||
|
boolean is = true;
|
||||||
|
for (int a : arr) {
|
||||||
|
is = a != 0;
|
||||||
|
if (!is) break;
|
||||||
|
}
|
||||||
|
if (is) {
|
||||||
|
do {
|
||||||
|
res += s.length() - i;
|
||||||
|
} while (--arr[s.charAt(left++) - 'a'] != 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue