java.lang.Object
g2701_2800.s2744_find_maximum_number_of_string_pairs.Solution

public class Solution extends Object
2744 - Find Maximum Number of String Pairs.<p>Easy</p> <p>You are given a <strong>0-indexed</strong> array <code>words</code> consisting of <strong>distinct</strong> strings.</p> <p>The string <code>words[i]</code> can be paired with the string <code>words[j]</code> if:</p> <ul> <li>The string <code>words[i]</code> is equal to the reversed string of <code>words[j]</code>.</li> <li><code>0 <= i < j < words.length</code>.</li> </ul> <p>Return <em>the <strong>maximum</strong> number of pairs that can be formed from the array</em> <code>words</code><em>.</em></p> <p>Note that each string can belong in <strong>at most one</strong> pair.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> words = [&ldquo;cd&rdquo;,&ldquo;ac&rdquo;,&ldquo;dc&rdquo;,&ldquo;ca&rdquo;,&ldquo;zz&rdquo;]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> In this example, we can form 2 pair of strings in the following way:</p> <ul> <li>We pair the 0<sup>th</sup> string with the 2<sup>nd</sup> string, as the reversed string of word[0] is &ldquo;dc&rdquo; and is equal to words[2].</li> <li>We pair the 1<sup>st</sup> string with the 3<sup>rd</sup> string, as the reversed string of word[1] is &ldquo;ca&rdquo; and is equal to words[3].</li> </ul> <p>It can be proven that 2 is the maximum number of pairs that can be formed.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> words = [&ldquo;ab&rdquo;,&ldquo;ba&rdquo;,&ldquo;cc&rdquo;]</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> In this example, we can form 1 pair of strings in the following way:</p> <ul> <li>We pair the 0<sup>th</sup> string with the 1<sup>st</sup> string, as the reversed string of words[1] is &ldquo;ab&rdquo; and is equal to words[0].</li> </ul> <p>It can be proven that 1 is the maximum number of pairs that can be formed.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> words = [&ldquo;aa&rdquo;,&ldquo;ab&rdquo;]</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> In this example, we are unable to form any pair of strings.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= words.length <= 50</code></li> <li><code>words[i].length == 2</code></li> <li><code>words</code> consists of distinct strings.</li> <li><code>words[i]</code> contains only lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maximumNumberOfStringPairs

      public int maximumNumberOfStringPairs(String[] words)