Skip to content

1358. Number of Substrings Containing All Three Characters 👍

Approach 1: Sliding window

  • Time: $O(n)$
  • Space: $O(3) = O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
 public:
  // Similar to 3. Longest Substring Without Repeating Characters
  int numberOfSubstrings(string s) {
    int ans = 0;
    vector<int> count(3);

    int l = 0;
    for (const char c : s) {
      ++count[c - 'a'];
      while (count[0] > 0 && count[1] > 0 && count[2] > 0)
        --count[s[l++] - 'a'];
      // s[0..r], s[1..r], ..., s[l - 1..r] are satified strings.
      ans += l;
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
  // Similar to 3. Longest SubString Without Repeating Characters
  public int numberOfSubstrings(String s) {
    int ans = 0;
    int[] count = new int[3];

    int l = 0;
    for (final char c : s.toCharArray()) {
      ++count[c - 'a'];
      while (count[0] > 0 && count[1] > 0 && count[2] > 0)
        --count[s.charAt(l++) - 'a'];
      // s[0..r], s[1..r], ..., s[l - 1..r] are satified strings.
      ans += l;
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution:
  # Similar to 3. Longest SubWithout Repeating Characters
  def numberOfSubstrings(self, s: str) -> int:
    ans = 0
    count = {c: 0 for c in 'abc'}

    l = 0
    for c in s:
      count[c] += 1
      while min(count.values()) > 0:
        count[s[l]] -= 1
        l += 1
      # s[0..r], s[1..r], ..., s[l - 1..r] are satified strings.
      ans += l

    return ans

Approach 2: Last seen

  • Time: $O(n)$
  • Space: $O(3) = O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
 public:
  // Similar to 3. Longest Substring Without Repeating Characters
  int numberOfSubstrings(string s) {
    int ans = 0;
    // lastSeen[c] := the index of the last time c appeared
    vector<int> lastSeen(3, -1);

    for (int i = 0; i < s.length(); ++i) {
      lastSeen[s[i] - 'a'] = i;
      // s[0..i], s[1..i], s[min(lastSeen)..i] are satisfied strings.
      ans += 1 + ranges::min(lastSeen);
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
  // Similar to 3. Longest SubString Without Repeating Characters
  public int numberOfSubstrings(String s) {
    int ans = 0;
    // lastSeen[c] := the index of the last time c appeared
    int[] lastSeen = new int[3];
    Arrays.fill(lastSeen, -1);

    for (int i = 0; i < s.length(); ++i) {
      lastSeen[s.charAt(i) - 'a'] = i;
      // s[0..i], s[1..i], s[Math.min(lastSeen)..i] are satisfied strings.
      ans += 1 + Arrays.stream(lastSeen).min().getAsInt();
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
  # Similar to 3. Longest SubWithout Repeating Characters
  def numberOfSubstrings(self, s: str) -> int:
    ans = 0
    # lastSeen[c] := the index of the last time c appeared
    lastSeen = {c: -1 for c in 'abc'}

    for i, c in enumerate(s):
      lastSeen[c] = i
      # s[0..i], s[1..i], s[min(lastSeen)..i] are satisfied strings.
      ans += 1 + min(lastSeen.values())

    return ans