Leetcode 2744: Find Maximum Number of String Pairs

grid47
grid47
Exploring patterns and algorithms
Feb 6, 2024 4 min read

You are given a list of distinct strings words, where each string consists of exactly two lowercase English letters. You are tasked with finding the maximum number of pairs of strings that can be formed where one string is the reverse of the other.
Problem
Approach
Steps
Complexity
Input: The input consists of a list of distinct strings `words` where each string has exactly two lowercase English letters.
Example: words = ["cd", "ac", "dc", "ca", "zz"]
Constraints:
• 1 <= words.length <= 50
• words[i].length == 2
• All strings in words are distinct.
• words[i] contains only lowercase English letters.
Output: Return the maximum number of pairs that can be formed where one string is the reverse of the other.
Example: Output: 2
Constraints:
Goal: To count the maximum number of pairs where one string is the reverse of another in the given list.
Steps:
• Create an array to track previously seen string pairs.
• For each string in the list, check if its reverse has already been seen.
• If it has been seen, increase the count of valid pairs.
Goal: The array `words` has at least one element and at most 50 elements, and each string is exactly two characters long.
Steps:
• 1 <= words.length <= 50
• words[i].length == 2
• words[i] contains only lowercase English letters
Assumptions:
• The list of strings will always be distinct.
Input: words = ["cd", "ac", "dc", "ca", "zz"]
Explanation: The two valid pairs are: ["cd", "dc"] and ["ac", "ca"]. Hence, the output is 2.

Input: words = ["ab", "ba", "cc"]
Explanation: The valid pair is ["ab", "ba"]. Hence, the output is 1.

Input: words = ["aa", "ab"]
Explanation: No valid pairs can be formed, so the output is 0.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus