ja / en
Overview

文字列のith以降で文字cが出現する最小のindex

2024/07/30
1 min read

Purpose

Returns the minimum index where character c appears at or after the i-th character of the string. Returns the string length if it doesn’t exist.

Time Complexity

O(n)O(n)

Usage

auto res = nextCharIndex(s);

s is a std::string. Use res[i][j] to get the index where character c appears at or after the i-th character (inclusive).

Implementation

vector<vector<int>> nextCharIndex(string &_s) {
vector m(_s.size() + 1, vector<int>(26));
for (int i = 0; i < 26; i++)
m[_s.size()][i] = _s.size();
for (int i = _s.size() - 1; i >= 0; i--)
for (int j = 0; j < 26; j++)
if (_s[i] - 'a' == j)
m[i][j] = i;
else
m[i][j] = m[i + 1][j];
return m;
}

Verify👍

https://atcoder.jp/contests/typical90/submissions/57632650