[LeetCode] 2744. Find Maximum Number of String Pairs
You are given a 0-indexed array words consisting of distinct strings.
The string words[i] can be paired with the string words[j] if:
The string words[i] is equal to the reversed string of words[j].
0 <= i < j < words.length.
Return the maximum number of pairs that can be formed from the array words.
Note that each string can belong in at most one pair.
Example 1:
Input: words = ["cd","ac","dc","ca","zz"]
Output: 2
Explanation: In this example, we can form 2 pair of strings in the following way:
- We pair the 0th string with the 2nd string, as the reversed string of word[0] is "dc" and is equal to words[2].
- We pair the 1st string with the 3rd string, as the reversed string of word[1] is "ca" and is equal to words[3].
It can be proven that 2 is the maximum number of pairs that can be formed.
Example 2:
Input: words = ["ab","ba","cc"]
Output: 1
Explanation: In this example, we can form 1 pair of strings in the following way:
- We pair the 0th string with the 1st string, as the reversed string of words[1] is "ab" and is equal to words[0].
It can be proven that 1 is the maximum number of pairs that can be formed.
Example 3:
Input: words = ["aa","ab"]
Output: 0
Explanation: In this example, we are unable to form any pair of strings.
Constraints:
1 <= words.length <= 50
words[i].length == 2
words consists of distinct strings.
words[i] contains only lowercase English letters.
最大字符串配对数目。
给你一个下标从 0 开始的数组 words ,数组中包含 互不相同 的字符串。
如果字符串 words[i] 与字符串 words[j] 满足以下条件,我们称它们可以匹配:
字符串 words[i] 等于 words[j] 的反转字符串。
0 <= i < j < words.length
请你返回数组 words 中的 最大 匹配数目。
注意,每个字符串最多匹配一次。
思路
题目给的是一些互不相同的字符串,所以一开始我用一个 hashset 把所有字符串存起来。因为题目要我们找的匹配字符串其实是找两个互为回文的字符串,所以这里我写一个 reverse 函数判断回文,对于某个单词,我得到他的回文之后去 hashset 判断这个回文是否存在于 hashset。注意这里有一个 corner case 是如果是类似 aaa 或者 zz 这种本身就是回文的单词,我们需要忽略。
复杂度
时间O(n)
空间O(n)
代码
Java实现
class Solution { public int maximumNumberOfStringPairs(String[] words) { Set<String> set = new HashSet<>(); for (String word : words) { if (!word.equals(reverse(word))) { set.add(word); } } int count = 0; for (String word : words) { if (set.contains(word) && set.contains(reverse(word))) { count++; set.remove(word); } } return count; } private String reverse(String s) { char[] letters = s.toCharArray(); int left = 0; int right = s.length() - 1; while (left < right) { char temp = letters[left]; letters[left] = letters[right]; letters[right] = temp; left++; right--; } return String.valueOf(letters); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2023-01-17 [LeetCode] 2536. Increment Submatrices by One
2023-01-17 [LeetCode] 1814. Count Nice Pairs in an Array
2021-01-17 [LeetCode] 1646. Get Maximum in Generated Array
2021-01-17 [LeetCode] 926. Flip String to Monotone Increasing