返回介绍

solution / 3000-3099 / 3042.Count Prefix and Suffix Pairs I / README_EN

发布于 2024-06-17 01:02:57 字数 8494 浏览 0 评论 0 收藏 0

3042. Count Prefix and Suffix Pairs I

中文文档

Description

You are given a 0-indexed string array words.

Let's define a boolean function isPrefixAndSuffix that takes two strings, str1 and str2:

  • isPrefixAndSuffix(str1, str2) returns true if str1 is both a prefix and a suffix of str2, and false otherwise.

For example, isPrefixAndSuffix("aba", "ababa") is true because "aba" is a prefix of "ababa" and also a suffix, but isPrefixAndSuffix("abc", "abcd") is false.

Return _an integer denoting the number of index pairs _(i, j)_ such that _i < j_, and _isPrefixAndSuffix(words[i], words[j])_ is _true_._

 

Example 1:

Input: words = ["a","aba","ababa","aa"]
Output: 4
Explanation: In this example, the counted index pairs are:
i = 0 and j = 1 because isPrefixAndSuffix("a", "aba") is true.
i = 0 and j = 2 because isPrefixAndSuffix("a", "ababa") is true.
i = 0 and j = 3 because isPrefixAndSuffix("a", "aa") is true.
i = 1 and j = 2 because isPrefixAndSuffix("aba", "ababa") is true.
Therefore, the answer is 4.

Example 2:

Input: words = ["pa","papa","ma","mama"]
Output: 2
Explanation: In this example, the counted index pairs are:
i = 0 and j = 1 because isPrefixAndSuffix("pa", "papa") is true.
i = 2 and j = 3 because isPrefixAndSuffix("ma", "mama") is true.
Therefore, the answer is 2.  

Example 3:

Input: words = ["abab","ab"]
Output: 0
Explanation: In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix("abab", "ab") is false.
Therefore, the answer is 0.

 

Constraints:

  • 1 <= words.length <= 50
  • 1 <= words[i].length <= 10
  • words[i] consists only of lowercase English letters.

Solutions

Solution 1: Enumeration

We can enumerate all index pairs $(i, j)$, where $i < j$, and then determine whether words[i] is a prefix or suffix of words[j]. If it is, we increment the count.

The time complexity is $O(n^2 \times m)$, where $n$ and $m$ are the length of words and the maximum length of the strings, respectively.

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
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;
  }
}
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;
  }
};
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
}
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: Trie

We can treat each string $s$ in the string array as a list of character pairs, where each character pair $(s[i], s[m - i - 1])$ represents the $i$th character pair of the prefix and suffix of string $s$.

We can use a trie to store all the character pairs, and then for each string $s$, we search for all the character pairs $(s[i], s[m - i - 1])$ in the trie, and add their counts to the answer.

The time complexity is $O(n \times m)$, and the space complexity is $O(n \times m)$. Here, $n$ and $m$ are the lengths of words and the maximum length of the strings, respectively.

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
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;
  }
}
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;
  }
};
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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文