题目:2744.最大字符串配对数目

​​题目来源:

        leetcode题目,网址:2744. 最大字符串配对数目 - 力扣(LeetCode)

解题思路:

       遍历数组,使用哈希集合记录当前字符串反转后的结果。对于数组中的每个字符串,若哈希集合中的已存在该字符串,则配对成功,计数加一,否则反转后加入哈希集合中。

解题代码:

class Solution {
    public int maximumNumberOfStringPairs(String[] words) {
        int res=0;
        Set reversed=new HashSet<>();
        for(String word:words){
            if(reversed.contains(word)){
                res++;
            }else{
                reversed.add(reverseString(word));
            }
        }
        return res;
    }
    public String reverseString(String str){
        return new StringBuffer(str).reverse().toString();
    }
}

总结:

        无官方题解。


你可能感兴趣的:(#,Java,leetcode,Java)