Skip to content

Latest commit

 

History

History
31 lines (26 loc) · 809 Bytes

Question_2744.md

File metadata and controls

31 lines (26 loc) · 809 Bytes

LeetCode Records - Question 2744 Find Maximum Number of String Pairs

Attempt 1: Use a HashSet to record strings

class Solution {
    public int maximumNumberOfStringPairs(String[] words) {
        Set<String> set = new HashSet<>();
        int count = 0;

        for (String word : words) {
            String reversedStr = getReversedString(word);
            if (set.contains(reversedStr)) {
                count++;
            } else {
                set.add(word);
            }
        }

        return count;
    }

    private String getReversedString(String str) {
        StringBuilder stringBuilder = new StringBuilder(str);
        return stringBuilder.reverse().toString();
    }
}
  • Runtime: 2 ms (Beats: 79.25%)
  • Memory: 43.32 MB (Beats: 44.81%)