File tree Expand file tree Collapse file tree 3 files changed +97
-0
lines changed
solution/3200-3299/3234.Count the Number of Substrings With Dominant Ones Expand file tree Collapse file tree 3 files changed +97
-0
lines changed Original file line number Diff line number Diff line change @@ -333,6 +333,40 @@ function numberOfSubstrings(s: string): number {
333333}
334334```
335335
336+ #### Rust
337+
338+ ``` rust
339+ impl Solution {
340+ pub fn number_of_substrings (s : String ) -> i32 {
341+ let n = s . len ();
342+ let mut nxt = vec! [n ; n + 1 ];
343+
344+ for i in (0 .. n ). rev () {
345+ nxt [i ] = nxt [i + 1 ];
346+ if & s [i .. i + 1 ] == " 0" {
347+ nxt [i ] = i ;
348+ }
349+ }
350+
351+ let mut ans = 0 ;
352+ for i in 0 .. n {
353+ let mut cnt0 = if & s [i .. i + 1 ] == " 0" { 1 } else { 0 };
354+ let mut j = i ;
355+ while j < n && (cnt0 * cnt0 ) as i64 <= n as i64 {
356+ let cnt1 = nxt [j + 1 ] - i - cnt0 ;
357+ if cnt1 >= (cnt0 * cnt0 ) {
358+ ans += std :: cmp :: min (nxt [j + 1 ] - j , cnt1 - cnt0 * cnt0 + 1 );
359+ }
360+ j = nxt [j + 1 ];
361+ cnt0 += 1 ;
362+ }
363+ }
364+
365+ ans as i32
366+ }
367+ }
368+ ```
369+
336370<!-- tabs: end -->
337371
338372<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -331,6 +331,40 @@ function numberOfSubstrings(s: string): number {
331331}
332332```
333333
334+ #### Rust
335+
336+ ``` rust
337+ impl Solution {
338+ pub fn number_of_substrings (s : String ) -> i32 {
339+ let n = s . len ();
340+ let mut nxt = vec! [n ; n + 1 ];
341+
342+ for i in (0 .. n ). rev () {
343+ nxt [i ] = nxt [i + 1 ];
344+ if & s [i .. i + 1 ] == " 0" {
345+ nxt [i ] = i ;
346+ }
347+ }
348+
349+ let mut ans = 0 ;
350+ for i in 0 .. n {
351+ let mut cnt0 = if & s [i .. i + 1 ] == " 0" { 1 } else { 0 };
352+ let mut j = i ;
353+ while j < n && (cnt0 * cnt0 ) as i64 <= n as i64 {
354+ let cnt1 = nxt [j + 1 ] - i - cnt0 ;
355+ if cnt1 >= (cnt0 * cnt0 ) {
356+ ans += std :: cmp :: min (nxt [j + 1 ] - j , cnt1 - cnt0 * cnt0 + 1 );
357+ }
358+ j = nxt [j + 1 ];
359+ cnt0 += 1 ;
360+ }
361+ }
362+
363+ ans as i32
364+ }
365+ }
366+ ```
367+
334368<!-- tabs: end -->
335369
336370<!-- solution: end -->
Original file line number Diff line number Diff line change 1+ impl Solution {
2+ pub fn number_of_substrings ( s : String ) -> i32 {
3+ let n = s. len ( ) ;
4+ let mut nxt = vec ! [ n; n + 1 ] ;
5+
6+ for i in ( 0 ..n) . rev ( ) {
7+ nxt[ i] = nxt[ i + 1 ] ;
8+ if & s[ i..i + 1 ] == "0" {
9+ nxt[ i] = i;
10+ }
11+ }
12+
13+ let mut ans = 0 ;
14+ for i in 0 ..n {
15+ let mut cnt0 = if & s[ i..i + 1 ] == "0" { 1 } else { 0 } ;
16+ let mut j = i;
17+ while j < n && ( cnt0 * cnt0) as i64 <= n as i64 {
18+ let cnt1 = nxt[ j + 1 ] - i - cnt0;
19+ if cnt1 >= ( cnt0 * cnt0) {
20+ ans += std:: cmp:: min ( nxt[ j + 1 ] - j, cnt1 - cnt0 * cnt0 + 1 ) ;
21+ }
22+ j = nxt[ j + 1 ] ;
23+ cnt0 += 1 ;
24+ }
25+ }
26+
27+ ans as i32
28+ }
29+ }
You can’t perform that action at this time.
0 commit comments