Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.3042,3045 #2350

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,194 @@ i = 2 且 j = 3 ,因为 isPrefixAndSuffix("ma", "mama") 为 true 。
<!-- tabs:start -->

```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<string>& 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;
}
```

<!-- tabs:end -->

### 方法二

<!-- tabs:start -->

```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<Integer, Node> 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<int, Node*> children;
int cnt;

Node()
: cnt(0) {}
};

class Solution {
public:
int countPrefixSuffixPairs(vector<string>& 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
}
```

<!-- tabs:end -->
Expand Down
175 changes: 175 additions & 0 deletions solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,194 @@ Therefore, the answer is 0.</pre>
<!-- tabs:start -->

```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<string>& 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;
}
```

<!-- tabs:end -->

### Solution 2

<!-- tabs:start -->

```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<Integer, Node> 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<int, Node*> children;
int cnt;

Node()
: cnt(0) {}
};

class Solution {
public:
int countPrefixSuffixPairs(vector<string>& 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
}
```

<!-- tabs:end -->
Expand Down
Loading
Loading