描述:
给你一个下标从 0 开始的数组 words
,数组中包含 互不相同 的字符串。
如果字符串 words[i]
与字符串 words[j]
满足以下条件,我们称它们可以匹配:
- 字符串
words[i]
等于words[j]
的反转字符串。 0 <= i < j < words.length
请你返回数组 words
中的 最大 匹配数目。
注意,每个字符串最多匹配一次。
示例 1:
输入:words = ["cd","ac","dc","ca","zz"] 输出:2 解释:在此示例中,我们可以通过以下方式匹配 2 对字符串: - 我们将第 0 个字符串与第 2 个字符串匹配,因为 word[0] 的反转字符串是 "dc" 并且等于 words[2]。 - 我们将第 1 个字符串与第 3 个字符串匹配,因为 word[1] 的反转字符串是 "ca" 并且等于 words[3]。 可以证明最多匹配数目是 2 。示例 2:
输入:words = ["ab","ba","cc"] 输出:1 解释:在此示例中,我们可以通过以下方式匹配 1 对字符串: - 我们将第 0 个字符串与第 1 个字符串匹配,因为 words[1] 的反转字符串 "ab" 与 words[0] 相等。 可以证明最多匹配数目是 1 。示例 3:
输入:words = ["aa","ab"] 输出:0 解释:这个例子中,无法匹配任何字符串。
思路:
依次加入字符串到新列表中,如果它的反转在新列表中且它的反转不等于它本身(这个是为了排除对称字符串的问题),则说明可以匹配,统计变量+1
代码:
class Solution(object):
def maximumNumberOfStringPairs(self, words):
"""
:type words: List[str]
:rtype: int
"""
wordslist=[]
count=0
for i in words:
wordslist.append(i)
if(i[::-1] in wordslist and i[::-1]!=i):
count+=1
return count
官方题解:
class Solution:
def maximumNumberOfStringPairs(self, words: List[str]) -> int:
n = len(words)
ans = 0
for i in range(n):
for j in range(i + 1, n):
if words[i] == words[j][::-1]:
ans += 1
return ans
java版本:
class Solution {
public int maximumNumberOfStringPairs(String[] words) {
int n = words.length;
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (words[i].charAt(0) == words[j].charAt(1) && words[i].charAt(1) == words[j].charAt(0)) {
++ans;
}
}
}
return ans;
}
}
C++版本:
class Solution {
public:
int maximumNumberOfStringPairs(vector<string>& words) {
int n = words.size();
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (words[i][0] == words[j][1] && words[i][1] == words[j][0]) {
++ans;
}
}
}
return ans;
}
};