LeetCode Biweekly Contest 20 3問目 - Number of Substrings Containing All Three Characters [1358]

Source

LeetCode Biweekly Contest 20
問題文

問題概要

省略

解法

省略

cLayversion 20200217-1)のコード

C++に変換後のコードはこちら

#define main dummy_main
{}
#undef main

class Solution {
public:
  int numberOfSubstrings(string s) {
    ll res = 0;
    int i, j, N = s.size();
    int a = 0, b = 0, c = 0;

    j = 0;
    rep(i,N){
      while(j < N && (a==0 || b==0 || c==0)){
        if(s[j]=='a') a++;
        if(s[j]=='b') b++;
        if(s[j]=='c') c++;
        j++;
      }
      if(a==0 || b==0 || c==0) break;
      res += N - j + 1;
      if(s[i]=='a') a--;
      if(s[i]=='b') b--;
      if(s[i]=='c') c--;
    }

    return res;
  }
};

Current time: 2024年04月23日21時10分23秒
Last modified: 2020年02月23日07時02分43秒 (by laycrs)
Tags: Competitive_Programming_Incomplete LeetCode
トップページに戻る

Logged in as: unknown user (not login)

ログイン: