From 7febd1c7a384eb2fb339f3e9ad3707360a79b72a Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 18 Feb 2024 16:23:00 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.3042,3045 * No.3042.Count Prefix and Suffix Pairs I * No.3045.Count Prefix and Suffix Pairs II --- .../README.md | 175 ++++++++++++++++++ .../README_EN.md | 175 ++++++++++++++++++ .../Solution.cpp | 17 ++ .../Solution.go | 10 + .../Solution.java | 16 ++ .../Solution.py | 7 + .../Solution.ts | 12 ++ .../Solution2.cpp | 30 +++ .../Solution2.go | 22 +++ .../Solution2.java | 23 +++ .../Solution2.py | 21 +++ .../Solution2.ts | 23 +++ .../README.md | 124 ++++++++++++- .../README_EN.md | 124 ++++++++++++- .../Solution.cpp | 30 +++ .../Solution.go | 22 +++ .../Solution.java | 23 +++ .../Solution.py | 21 +++ .../Solution.ts | 23 +++ 19 files changed, 892 insertions(+), 6 deletions(-) create mode 100644 solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.cpp create mode 100644 solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.go create mode 100644 solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.java create mode 100644 solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.py create mode 100644 solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.ts create mode 100644 solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.cpp create mode 100644 solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.go create mode 100644 solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.java create mode 100644 solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.py create mode 100644 solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.ts create mode 100644 solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.cpp create mode 100644 solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.go create mode 100644 solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.java create mode 100644 solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.py create mode 100644 solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.ts diff --git a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README.md b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README.md index ecb370e855cd4..bb06d2d444921 100644 --- a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README.md +++ b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README.md @@ -67,19 +67,194 @@ i = 2 且 j = 3 ,因为 isPrefixAndSuffix("ma", "mama") 为 true 。 ```python +class Solution: + def countPrefixSuffixPairs(self, words: List[str]) -> int: + ans = 0 + for i, s in enumerate(words): + for t in words[i + 1 :]: + ans += t.endswith(s) and t.startswith(s) + return ans +``` + +```java +class Solution { + public int countPrefixSuffixPairs(String[] words) { + int ans = 0; + int n = words.length; + for (int i = 0; i < n; ++i) { + String s = words[i]; + for (int j = i + 1; j < n; ++j) { + String t = words[j]; + if (t.startsWith(s) && t.endsWith(s)) { + ++ans; + } + } + } + return ans; + } +} +``` + +```cpp +class Solution { +public: + int countPrefixSuffixPairs(vector& words) { + int ans = 0; + int n = words.size(); + for (int i = 0; i < n; ++i) { + string s = words[i]; + for (int j = i + 1; j < n; ++j) { + string t = words[j]; + if (t.find(s) == 0 && t.rfind(s) == t.length() - s.length()) { + ++ans; + } + } + } + return ans; + } +}; +``` + +```go +func countPrefixSuffixPairs(words []string) (ans int) { + for i, s := range words { + for _, t := range words[i+1:] { + if strings.HasPrefix(t, s) && strings.HasSuffix(t, s) { + ans++ + } + } + } + return +} +``` + +```ts +function countPrefixSuffixPairs(words: string[]): number { + let ans = 0; + for (let i = 0; i < words.length; ++i) { + const s = words[i]; + for (const t of words.slice(i + 1)) { + if (t.startsWith(s) && t.endsWith(s)) { + ++ans; + } + } + } + return ans; +} +``` + + + +### 方法二 + + +```python +class Node: + __slots__ = ["children", "cnt"] + + def __init__(self): + self.children = {} + self.cnt = 0 + + +class Solution: + def countPrefixSuffixPairs(self, words: List[str]) -> int: + ans = 0 + trie = Node() + for s in words: + node = trie + for p in zip(s, reversed(s)): + if p not in node.children: + node.children[p] = Node() + node = node.children[p] + ans += node.cnt + node.cnt += 1 + return ans ``` ```java +class Node { + Map children = new HashMap<>(); + int cnt; +} +class Solution { + public int countPrefixSuffixPairs(String[] words) { + int ans = 0; + Node trie = new Node(); + for (String s : words) { + Node node = trie; + int m = s.length(); + for (int i = 0; i < m; ++i) { + int p = s.charAt(i) * 32 + s.charAt(m - i - 1); + node.children.putIfAbsent(p, new Node()); + node = node.children.get(p); + ans += node.cnt; + } + ++node.cnt; + } + return ans; + } +} ``` ```cpp +class Node { +public: + unordered_map children; + int cnt; + + Node() + : cnt(0) {} +}; +class Solution { +public: + int countPrefixSuffixPairs(vector& words) { + int ans = 0; + Node* trie = new Node(); + for (const string& s : words) { + Node* node = trie; + int m = s.length(); + for (int i = 0; i < m; ++i) { + int p = s[i] * 32 + s[m - i - 1]; + if (node->children.find(p) == node->children.end()) { + node->children[p] = new Node(); + } + node = node->children[p]; + ans += node->cnt; + } + ++node->cnt; + } + return ans; + } +}; ``` ```go +type Node struct { + children map[int]*Node + cnt int +} +func countPrefixSuffixPairs(words []string) (ans int) { + trie := &Node{children: make(map[int]*Node)} + for _, s := range words { + node := trie + m := len(s) + for i := 0; i < m; i++ { + p := int(s[i])*32 + int(s[m-i-1]) + if _, ok := node.children[p]; !ok { + node.children[p] = &Node{children: make(map[int]*Node)} + } + node = node.children[p] + ans += node.cnt + } + node.cnt++ + } + return +} ``` diff --git a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README_EN.md b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README_EN.md index 9f1ff53778c9d..548d00fb5a388 100644 --- a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README_EN.md +++ b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README_EN.md @@ -63,19 +63,194 @@ Therefore, the answer is 0. ```python +class Solution: + def countPrefixSuffixPairs(self, words: List[str]) -> int: + ans = 0 + for i, s in enumerate(words): + for t in words[i + 1 :]: + ans += t.endswith(s) and t.startswith(s) + return ans +``` + +```java +class Solution { + public int countPrefixSuffixPairs(String[] words) { + int ans = 0; + int n = words.length; + for (int i = 0; i < n; ++i) { + String s = words[i]; + for (int j = i + 1; j < n; ++j) { + String t = words[j]; + if (t.startsWith(s) && t.endsWith(s)) { + ++ans; + } + } + } + return ans; + } +} +``` + +```cpp +class Solution { +public: + int countPrefixSuffixPairs(vector& words) { + int ans = 0; + int n = words.size(); + for (int i = 0; i < n; ++i) { + string s = words[i]; + for (int j = i + 1; j < n; ++j) { + string t = words[j]; + if (t.find(s) == 0 && t.rfind(s) == t.length() - s.length()) { + ++ans; + } + } + } + return ans; + } +}; +``` + +```go +func countPrefixSuffixPairs(words []string) (ans int) { + for i, s := range words { + for _, t := range words[i+1:] { + if strings.HasPrefix(t, s) && strings.HasSuffix(t, s) { + ans++ + } + } + } + return +} +``` + +```ts +function countPrefixSuffixPairs(words: string[]): number { + let ans = 0; + for (let i = 0; i < words.length; ++i) { + const s = words[i]; + for (const t of words.slice(i + 1)) { + if (t.startsWith(s) && t.endsWith(s)) { + ++ans; + } + } + } + return ans; +} +``` + + + +### Solution 2 + + +```python +class Node: + __slots__ = ["children", "cnt"] + + def __init__(self): + self.children = {} + self.cnt = 0 + + +class Solution: + def countPrefixSuffixPairs(self, words: List[str]) -> int: + ans = 0 + trie = Node() + for s in words: + node = trie + for p in zip(s, reversed(s)): + if p not in node.children: + node.children[p] = Node() + node = node.children[p] + ans += node.cnt + node.cnt += 1 + return ans ``` ```java +class Node { + Map children = new HashMap<>(); + int cnt; +} +class Solution { + public int countPrefixSuffixPairs(String[] words) { + int ans = 0; + Node trie = new Node(); + for (String s : words) { + Node node = trie; + int m = s.length(); + for (int i = 0; i < m; ++i) { + int p = s.charAt(i) * 32 + s.charAt(m - i - 1); + node.children.putIfAbsent(p, new Node()); + node = node.children.get(p); + ans += node.cnt; + } + ++node.cnt; + } + return ans; + } +} ``` ```cpp +class Node { +public: + unordered_map children; + int cnt; + + Node() + : cnt(0) {} +}; +class Solution { +public: + int countPrefixSuffixPairs(vector& words) { + int ans = 0; + Node* trie = new Node(); + for (const string& s : words) { + Node* node = trie; + int m = s.length(); + for (int i = 0; i < m; ++i) { + int p = s[i] * 32 + s[m - i - 1]; + if (node->children.find(p) == node->children.end()) { + node->children[p] = new Node(); + } + node = node->children[p]; + ans += node->cnt; + } + ++node->cnt; + } + return ans; + } +}; ``` ```go +type Node struct { + children map[int]*Node + cnt int +} +func countPrefixSuffixPairs(words []string) (ans int) { + trie := &Node{children: make(map[int]*Node)} + for _, s := range words { + node := trie + m := len(s) + for i := 0; i < m; i++ { + p := int(s[i])*32 + int(s[m-i-1]) + if _, ok := node.children[p]; !ok { + node.children[p] = &Node{children: make(map[int]*Node)} + } + node = node.children[p] + ans += node.cnt + } + node.cnt++ + } + return +} ``` diff --git a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.cpp b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.cpp new file mode 100644 index 0000000000000..d701ad5329ea8 --- /dev/null +++ b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int countPrefixSuffixPairs(vector& words) { + int ans = 0; + int n = words.size(); + for (int i = 0; i < n; ++i) { + string s = words[i]; + for (int j = i + 1; j < n; ++j) { + string t = words[j]; + if (t.find(s) == 0 && t.rfind(s) == t.length() - s.length()) { + ++ans; + } + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.go b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.go new file mode 100644 index 0000000000000..6204b9266e845 --- /dev/null +++ b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.go @@ -0,0 +1,10 @@ +func countPrefixSuffixPairs(words []string) (ans int) { + for i, s := range words { + for _, t := range words[i+1:] { + if strings.HasPrefix(t, s) && strings.HasSuffix(t, s) { + ans++ + } + } + } + return +} \ No newline at end of file diff --git a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.java b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.java new file mode 100644 index 0000000000000..a9291da5013c8 --- /dev/null +++ b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.java @@ -0,0 +1,16 @@ +class Solution { + public int countPrefixSuffixPairs(String[] words) { + int ans = 0; + int n = words.length; + for (int i = 0; i < n; ++i) { + String s = words[i]; + for (int j = i + 1; j < n; ++j) { + String t = words[j]; + if (t.startsWith(s) && t.endsWith(s)) { + ++ans; + } + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.py b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.py new file mode 100644 index 0000000000000..f7556ea4b350f --- /dev/null +++ b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.py @@ -0,0 +1,7 @@ +class Solution: + def countPrefixSuffixPairs(self, words: List[str]) -> int: + ans = 0 + for i, s in enumerate(words): + for t in words[i + 1 :]: + ans += t.endswith(s) and t.startswith(s) + return ans diff --git a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.ts b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.ts new file mode 100644 index 0000000000000..5afa25c0db062 --- /dev/null +++ b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution.ts @@ -0,0 +1,12 @@ +function countPrefixSuffixPairs(words: string[]): number { + let ans = 0; + for (let i = 0; i < words.length; ++i) { + const s = words[i]; + for (const t of words.slice(i + 1)) { + if (t.startsWith(s) && t.endsWith(s)) { + ++ans; + } + } + } + return ans; +} diff --git a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.cpp b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.cpp new file mode 100644 index 0000000000000..36ccab2d74c28 --- /dev/null +++ b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.cpp @@ -0,0 +1,30 @@ +class Node { +public: + unordered_map children; + int cnt; + + Node() + : cnt(0) {} +}; + +class Solution { +public: + int countPrefixSuffixPairs(vector& words) { + int ans = 0; + Node* trie = new Node(); + for (const string& s : words) { + Node* node = trie; + int m = s.length(); + for (int i = 0; i < m; ++i) { + int p = s[i] * 32 + s[m - i - 1]; + if (node->children.find(p) == node->children.end()) { + node->children[p] = new Node(); + } + node = node->children[p]; + ans += node->cnt; + } + ++node->cnt; + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.go b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.go new file mode 100644 index 0000000000000..1ef158e40998e --- /dev/null +++ b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.go @@ -0,0 +1,22 @@ +type Node struct { + children map[int]*Node + cnt int +} + +func countPrefixSuffixPairs(words []string) (ans int) { + trie := &Node{children: make(map[int]*Node)} + for _, s := range words { + node := trie + m := len(s) + for i := 0; i < m; i++ { + p := int(s[i])*32 + int(s[m-i-1]) + if _, ok := node.children[p]; !ok { + node.children[p] = &Node{children: make(map[int]*Node)} + } + node = node.children[p] + ans += node.cnt + } + node.cnt++ + } + return +} \ No newline at end of file diff --git a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.java b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.java new file mode 100644 index 0000000000000..03ceadc13c38d --- /dev/null +++ b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.java @@ -0,0 +1,23 @@ +class Node { + Map children = new HashMap<>(); + int cnt; +} + +class Solution { + public int countPrefixSuffixPairs(String[] words) { + int ans = 0; + Node trie = new Node(); + for (String s : words) { + Node node = trie; + int m = s.length(); + for (int i = 0; i < m; ++i) { + int p = s.charAt(i) * 32 + s.charAt(m - i - 1); + node.children.putIfAbsent(p, new Node()); + node = node.children.get(p); + ans += node.cnt; + } + ++node.cnt; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.py b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.py new file mode 100644 index 0000000000000..0bcc0fc6597d8 --- /dev/null +++ b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.py @@ -0,0 +1,21 @@ +class Node: + __slots__ = ["children", "cnt"] + + def __init__(self): + self.children = {} + self.cnt = 0 + + +class Solution: + def countPrefixSuffixPairs(self, words: List[str]) -> int: + ans = 0 + trie = Node() + for s in words: + node = trie + for p in zip(s, reversed(s)): + if p not in node.children: + node.children[p] = Node() + node = node.children[p] + ans += node.cnt + node.cnt += 1 + return ans diff --git a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.ts b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.ts new file mode 100644 index 0000000000000..a0c3c05c43942 --- /dev/null +++ b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/Solution2.ts @@ -0,0 +1,23 @@ +class Node { + children: Map = new Map(); + cnt: number = 0; +} + +function countPrefixSuffixPairs(words: string[]): number { + let ans: number = 0; + const trie: Node = new Node(); + for (const s of words) { + let node: Node = trie; + const m: number = s.length; + for (let i: number = 0; i < m; ++i) { + const p: number = s.charCodeAt(i) * 32 + s.charCodeAt(m - i - 1); + if (!node.children.has(p)) { + node.children.set(p, new Node()); + } + node = node.children.get(p)!; + ans += node.cnt; + } + ++node.cnt; + } + return ans; +} diff --git a/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/README.md b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/README.md index 036ce4dc1404f..a250542748afe 100644 --- a/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/README.md +++ b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/README.md @@ -68,19 +68,137 @@ i = 2 且 j = 3 ,因为 isPrefixAndSuffix("ma", "mama") 为 true 。 ```python - +class Node: + __slots__ = ["children", "cnt"] + + def __init__(self): + self.children = {} + self.cnt = 0 + + +class Solution: + def countPrefixSuffixPairs(self, words: List[str]) -> int: + ans = 0 + trie = Node() + for s in words: + node = trie + for p in zip(s, reversed(s)): + if p not in node.children: + node.children[p] = Node() + node = node.children[p] + ans += node.cnt + node.cnt += 1 + return ans ``` ```java - +class Node { + Map children = new HashMap<>(); + int cnt; +} + +class Solution { + public long countPrefixSuffixPairs(String[] words) { + long ans = 0; + Node trie = new Node(); + for (String s : words) { + Node node = trie; + int m = s.length(); + for (int i = 0; i < m; ++i) { + int p = s.charAt(i) * 32 + s.charAt(m - i - 1); + node.children.putIfAbsent(p, new Node()); + node = node.children.get(p); + ans += node.cnt; + } + ++node.cnt; + } + return ans; + } +} ``` ```cpp - +class Node { +public: + unordered_map children; + int cnt; + + Node() + : cnt(0) {} +}; + +class Solution { +public: + long long countPrefixSuffixPairs(vector& words) { + long long ans = 0; + Node* trie = new Node(); + for (const string& s : words) { + Node* node = trie; + int m = s.length(); + for (int i = 0; i < m; ++i) { + int p = s[i] * 32 + s[m - i - 1]; + if (node->children.find(p) == node->children.end()) { + node->children[p] = new Node(); + } + node = node->children[p]; + ans += node->cnt; + } + ++node->cnt; + } + return ans; + } +}; ``` ```go +type Node struct { + children map[int]*Node + cnt int +} + +func countPrefixSuffixPairs(words []string) (ans int64) { + trie := &Node{children: make(map[int]*Node)} + for _, s := range words { + node := trie + m := len(s) + for i := 0; i < m; i++ { + p := int(s[i])*32 + int(s[m-i-1]) + if _, ok := node.children[p]; !ok { + node.children[p] = &Node{children: make(map[int]*Node)} + } + node = node.children[p] + ans += int64(node.cnt) + } + node.cnt++ + } + return +} +``` +```ts +class Node { + children: Map = new Map(); + cnt: number = 0; +} + +function countPrefixSuffixPairs(words: string[]): number { + let ans: number = 0; + const trie: Node = new Node(); + for (const s of words) { + let node: Node = trie; + const m: number = s.length; + for (let i: number = 0; i < m; ++i) { + const p: number = s.charCodeAt(i) * 32 + s.charCodeAt(m - i - 1); + if (!node.children.has(p)) { + node.children.set(p, new Node()); + } + node = node.children.get(p)!; + ans += node.cnt; + } + ++node.cnt; + } + return ans; +} ``` diff --git a/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/README_EN.md b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/README_EN.md index 6fe5f0b4044ac..2a405e721cd50 100644 --- a/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/README_EN.md +++ b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/README_EN.md @@ -64,19 +64,137 @@ Therefore, the answer is 0. ```python - +class Node: + __slots__ = ["children", "cnt"] + + def __init__(self): + self.children = {} + self.cnt = 0 + + +class Solution: + def countPrefixSuffixPairs(self, words: List[str]) -> int: + ans = 0 + trie = Node() + for s in words: + node = trie + for p in zip(s, reversed(s)): + if p not in node.children: + node.children[p] = Node() + node = node.children[p] + ans += node.cnt + node.cnt += 1 + return ans ``` ```java - +class Node { + Map children = new HashMap<>(); + int cnt; +} + +class Solution { + public long countPrefixSuffixPairs(String[] words) { + long ans = 0; + Node trie = new Node(); + for (String s : words) { + Node node = trie; + int m = s.length(); + for (int i = 0; i < m; ++i) { + int p = s.charAt(i) * 32 + s.charAt(m - i - 1); + node.children.putIfAbsent(p, new Node()); + node = node.children.get(p); + ans += node.cnt; + } + ++node.cnt; + } + return ans; + } +} ``` ```cpp - +class Node { +public: + unordered_map children; + int cnt; + + Node() + : cnt(0) {} +}; + +class Solution { +public: + long long countPrefixSuffixPairs(vector& words) { + long long ans = 0; + Node* trie = new Node(); + for (const string& s : words) { + Node* node = trie; + int m = s.length(); + for (int i = 0; i < m; ++i) { + int p = s[i] * 32 + s[m - i - 1]; + if (node->children.find(p) == node->children.end()) { + node->children[p] = new Node(); + } + node = node->children[p]; + ans += node->cnt; + } + ++node->cnt; + } + return ans; + } +}; ``` ```go +type Node struct { + children map[int]*Node + cnt int +} + +func countPrefixSuffixPairs(words []string) (ans int64) { + trie := &Node{children: make(map[int]*Node)} + for _, s := range words { + node := trie + m := len(s) + for i := 0; i < m; i++ { + p := int(s[i])*32 + int(s[m-i-1]) + if _, ok := node.children[p]; !ok { + node.children[p] = &Node{children: make(map[int]*Node)} + } + node = node.children[p] + ans += int64(node.cnt) + } + node.cnt++ + } + return +} +``` +```ts +class Node { + children: Map = new Map(); + cnt: number = 0; +} + +function countPrefixSuffixPairs(words: string[]): number { + let ans: number = 0; + const trie: Node = new Node(); + for (const s of words) { + let node: Node = trie; + const m: number = s.length; + for (let i: number = 0; i < m; ++i) { + const p: number = s.charCodeAt(i) * 32 + s.charCodeAt(m - i - 1); + if (!node.children.has(p)) { + node.children.set(p, new Node()); + } + node = node.children.get(p)!; + ans += node.cnt; + } + ++node.cnt; + } + return ans; +} ``` diff --git a/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.cpp b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.cpp new file mode 100644 index 0000000000000..052dc803a9c94 --- /dev/null +++ b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.cpp @@ -0,0 +1,30 @@ +class Node { +public: + unordered_map children; + int cnt; + + Node() + : cnt(0) {} +}; + +class Solution { +public: + long long countPrefixSuffixPairs(vector& words) { + long long ans = 0; + Node* trie = new Node(); + for (const string& s : words) { + Node* node = trie; + int m = s.length(); + for (int i = 0; i < m; ++i) { + int p = s[i] * 32 + s[m - i - 1]; + if (node->children.find(p) == node->children.end()) { + node->children[p] = new Node(); + } + node = node->children[p]; + ans += node->cnt; + } + ++node->cnt; + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.go b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.go new file mode 100644 index 0000000000000..c6804fcf82c88 --- /dev/null +++ b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.go @@ -0,0 +1,22 @@ +type Node struct { + children map[int]*Node + cnt int +} + +func countPrefixSuffixPairs(words []string) (ans int64) { + trie := &Node{children: make(map[int]*Node)} + for _, s := range words { + node := trie + m := len(s) + for i := 0; i < m; i++ { + p := int(s[i])*32 + int(s[m-i-1]) + if _, ok := node.children[p]; !ok { + node.children[p] = &Node{children: make(map[int]*Node)} + } + node = node.children[p] + ans += int64(node.cnt) + } + node.cnt++ + } + return +} \ No newline at end of file diff --git a/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.java b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.java new file mode 100644 index 0000000000000..4a29d29ec449a --- /dev/null +++ b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.java @@ -0,0 +1,23 @@ +class Node { + Map children = new HashMap<>(); + int cnt; +} + +class Solution { + public long countPrefixSuffixPairs(String[] words) { + long ans = 0; + Node trie = new Node(); + for (String s : words) { + Node node = trie; + int m = s.length(); + for (int i = 0; i < m; ++i) { + int p = s.charAt(i) * 32 + s.charAt(m - i - 1); + node.children.putIfAbsent(p, new Node()); + node = node.children.get(p); + ans += node.cnt; + } + ++node.cnt; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.py b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.py new file mode 100644 index 0000000000000..0bcc0fc6597d8 --- /dev/null +++ b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.py @@ -0,0 +1,21 @@ +class Node: + __slots__ = ["children", "cnt"] + + def __init__(self): + self.children = {} + self.cnt = 0 + + +class Solution: + def countPrefixSuffixPairs(self, words: List[str]) -> int: + ans = 0 + trie = Node() + for s in words: + node = trie + for p in zip(s, reversed(s)): + if p not in node.children: + node.children[p] = Node() + node = node.children[p] + ans += node.cnt + node.cnt += 1 + return ans diff --git a/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.ts b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.ts new file mode 100644 index 0000000000000..a0c3c05c43942 --- /dev/null +++ b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/Solution.ts @@ -0,0 +1,23 @@ +class Node { + children: Map = new Map(); + cnt: number = 0; +} + +function countPrefixSuffixPairs(words: string[]): number { + let ans: number = 0; + const trie: Node = new Node(); + for (const s of words) { + let node: Node = trie; + const m: number = s.length; + for (let i: number = 0; i < m; ++i) { + const p: number = s.charCodeAt(i) * 32 + s.charCodeAt(m - i - 1); + if (!node.children.has(p)) { + node.children.set(p, new Node()); + } + node = node.children.get(p)!; + ans += node.cnt; + } + ++node.cnt; + } + return ans; +}