diff --git a/solving/number-of-wonderful-substrings/.url b/solving/number-of-wonderful-substrings/.url new file mode 100644 index 0000000..794df3d --- /dev/null +++ b/solving/number-of-wonderful-substrings/.url @@ -0,0 +1 @@ +https://leetcode.com/problems/number-of-wonderful-substrings/ diff --git a/solving/number-of-wonderful-substrings/driver.h b/solving/number-of-wonderful-substrings/driver.h new file mode 100644 index 0000000..248b8e9 --- /dev/null +++ b/solving/number-of-wonderful-substrings/driver.h @@ -0,0 +1,13 @@ +// driver.h - generated by 'usaco new' from LeetCode's metadata (edit freely). +// Reads number-of-wonderful-substrings.in in LeetCode's testcase format, cases back to back: +// one argument per line +// and writes one answer per case to number-of-wonderful-substrings.out, formatted the way LeetCode +// shows its output. Parsing/printing helpers: leetcode.h. +int main() { + freopen("number-of-wonderful-substrings.in", "r", stdin); + freopen("number-of-wonderful-substrings.out", "w", stdout); + for (vector in; lc::read_case(in, 1);) { + auto word = lc::parse(in[0]); + cout << lc::show(Solution().wonderfulSubstrings(word)) << '\n'; + } +} diff --git a/solving/number-of-wonderful-substrings/main.cpp b/solving/number-of-wonderful-substrings/main.cpp new file mode 100644 index 0000000..d46d9f9 --- /dev/null +++ b/solving/number-of-wonderful-substrings/main.cpp @@ -0,0 +1,139 @@ +#include +using namespace std; +#ifdef LOCAL // set by ./usaco builds only, so this is safe to submit as-is +#include "debugging.h" +#include "leetcode.h" // ListNode, TreeNode +#endif + + +// ----- TYPES ----- +typedef long long ll; +typedef pair pii; +typedef vector vi; + +// ----- FILE IO ----- +#define STDIN(filename) freopen(filename, "r", stdin) +#define STDOUT(filename) freopen(filename, "w", stdout) +#define FASTIO cin.tie(0)->sync_with_stdio(0) +#define STDIO(filename) \ + FASTIO; \ + string filenamee(filename); \ + STDIN((filenamee + ".in").c_str()); \ + STDOUT((filenamee + ".out").c_str()) +#define NL '\n' +#define CM ", " +#define SP " " + +// ----- FOR LOOPS ----- +#define F(i, L, R) for (int i = L; i < R; i++) +#define FE(i, L, R) for (int i = L; i <= R; i++) +#define FD(i, L, R) for (int i = L; i > R; i--) +#define FDE(i, L, R) for (int i = L; i >= R; i--) + +// ----- WEZ ----- +#define takeI(n) \ + int n; \ + cin >> n +#define takeII(n, m) \ + int n, m; \ + cin >> n >> m +#define takeIII(n, m, k) \ + int n, m, k; \ + cin >> n >> m >> k +#define takeL(n) \ + ll n; \ + cin >> n +#define takeS(s) \ + string s; \ + cin >> s +#define takeV(v, n) \ + vi v(n); \ + for (auto &x : v) \ + cin >> x +#define takeVL(v, n) \ + vector v(n); \ + for (auto &x : v) \ + cin >> x +#define whileZ \ + takeI(T); \ + while (T--) +// +// ----- MAX MIN ----- +#define REMAX(a, b) (a) = max((a), (b)) +#define REMIN(a, b) (a) = min((a), (b)) + +// ----- PAIRS ----- +#define fi first +#define se second + +// ----- OUTPUT ----- +#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL + +// ----- DEBUG ----- (compiled out on the judge) +#ifdef LOCAL +#define DBG(vari) cerr << #vari << " = " << (vari) << endl +#define DBG2(a, b) cerr << #a "=" << (a) << CM << #b "=" << (b) << endl +#define printA(a, L, R) FE(i, L, R) cerr << a[i] << (i == R ? '\n' : ' ') +#define printV(a) printA(a, 0, SZ(a) - 1) +#else +#define DBG(vari) +#define DBG2(a, b) +#define printA(a, L, R) +#define printV(a) +#endif + +// ----- STL DS +#define ALL(c) (c).begin(), (c).end() +#define SZ(c) ((int)(c).size()) +#define SORT(c) sort(ALL(c)) +#define RSORT(c) sort(ALL(c), greater<>()) +#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end()) +#define pb push_back + +class Solution { +public: + long long wonderfulSubstrings(string s) { + /** + Let x be a bitmask for the parity of a string s + A string is wonderful if the bitmask has less than 2 ones + + For example, "ccjjc" and "abab" are wonderful, but "ab" is not. + abcj + 0010 - Wonderful + 0000 - Wonderful + 1100 - Not wonderful + + Given a string word that consists 'a' through 'j' + return the number of wonderful non-empty substrings in word + If the same substring appears multiple times in word + then count each occurrence separately. + + "aabb" + -> 0 1 2 3 + "a" -> 0 1000 + "aa" -> 1 0000 , 1000 + "aab" -> 2 0100 , 1100 , 0100 + "aabb" -> 3 0000 , 1000 , 0000, 0100 + + T(0, j) = T(0, j-1) ^ s[j] + */ + vector counts(1024, 0); + counts[0] = 1; + int prefix = 0; + long long res = 0; + for (int i = 0; i < s.size(); i++) { + prefix ^= 1 << (s[i] - 'a'); + + res += counts[prefix]; + for (int i = 0; i < 10; i++) { + res += counts[prefix ^ (1 << i)]; + } + counts[prefix]++; + } + return res; + } +}; + +#ifdef LOCAL // runs the examples; see driver.h +#include "driver.h" +#endif diff --git a/solving/number-of-wonderful-substrings/number-of-wonderful-substrings.in b/solving/number-of-wonderful-substrings/number-of-wonderful-substrings.in new file mode 100644 index 0000000..2480333 --- /dev/null +++ b/solving/number-of-wonderful-substrings/number-of-wonderful-substrings.in @@ -0,0 +1,3 @@ +"aba" +"aabb" +"he" diff --git a/solving/number-of-wonderful-substrings/problem.md b/solving/number-of-wonderful-substrings/problem.md new file mode 100644 index 0000000..731189b --- /dev/null +++ b/solving/number-of-wonderful-substrings/problem.md @@ -0,0 +1,53 @@ +# 1915. Number of Wonderful Substrings + +A **wonderful** string is a string where **at most one** letter appears an **odd** number of times. + +- For example, `"ccjjc"` and `"abab"` are wonderful, but `"ab"` is not. + +Given a string `word` that consists of the first ten lowercase English letters (`'a'` through `'j'`), return *the **number of wonderful non-empty substrings** in* `word`*. If the same substring appears multiple times in* `word`*, then count **each occurrence** separately.* + +A **substring** is a contiguous sequence of characters in a string. + +**Example 1:** + +``` +Input: word = "aba" +Output: 4 +Explanation: The four wonderful substrings are underlined below: +- "aba" -> "a" +- "aba" -> "b" +- "aba" -> "a" +- "aba" -> "aba" +``` + +**Example 2:** + +``` +Input: word = "aabb" +Output: 9 +Explanation: The nine wonderful substrings are underlined below: +- "aabb" -> "a" +- "aabb" -> "aa" +- "aabb" -> "aab" +- "aabb" -> "aabb" +- "aabb" -> "a" +- "aabb" -> "abb" +- "aabb" -> "b" +- "aabb" -> "bb" +- "aabb" -> "b" +``` + +**Example 3:** + +``` +Input: word = "he" +Output: 2 +Explanation: The two wonderful substrings are underlined below: +- "he" -> "h" +- "he" -> "e" +``` + +**Constraints:** + +- `1 <= word.length <= 10^5` +- `word` consists of lowercase English letters from `'a'` to `'j'`. diff --git a/solving/number-of-wonderful-substrings/tests/1.in b/solving/number-of-wonderful-substrings/tests/1.in new file mode 100644 index 0000000..3885a18 --- /dev/null +++ b/solving/number-of-wonderful-substrings/tests/1.in @@ -0,0 +1 @@ +"aba" diff --git a/solving/number-of-wonderful-substrings/tests/1.out b/solving/number-of-wonderful-substrings/tests/1.out new file mode 100644 index 0000000..b8626c4 --- /dev/null +++ b/solving/number-of-wonderful-substrings/tests/1.out @@ -0,0 +1 @@ +4 diff --git a/solving/number-of-wonderful-substrings/tests/2.in b/solving/number-of-wonderful-substrings/tests/2.in new file mode 100644 index 0000000..9857446 --- /dev/null +++ b/solving/number-of-wonderful-substrings/tests/2.in @@ -0,0 +1 @@ +"aabb" diff --git a/solving/number-of-wonderful-substrings/tests/2.out b/solving/number-of-wonderful-substrings/tests/2.out new file mode 100644 index 0000000..ec63514 --- /dev/null +++ b/solving/number-of-wonderful-substrings/tests/2.out @@ -0,0 +1 @@ +9 diff --git a/solving/number-of-wonderful-substrings/tests/3.in b/solving/number-of-wonderful-substrings/tests/3.in new file mode 100644 index 0000000..d590b6a --- /dev/null +++ b/solving/number-of-wonderful-substrings/tests/3.in @@ -0,0 +1 @@ +"he" diff --git a/solving/number-of-wonderful-substrings/tests/3.out b/solving/number-of-wonderful-substrings/tests/3.out new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/solving/number-of-wonderful-substrings/tests/3.out @@ -0,0 +1 @@ +2