最大字符串配对数目 (Find Maximum Number of String Pairs)

 

思路:暴力解法

// @Title: 最大字符串配对数目 (Find Maximum Number of String Pairs)
// @Author: qisiii
// @Date: 2024-01-17 21:22:23
// @Runtime: 1 ms
// @Memory: 41.2 MB
// @comment: 暴力解法
// @flag: WHITE
class Solution {
    public int maximumNumberOfStringPairs(String[] words) {
        int result = 0;
        for (int i = 0; i < words.length; i++) {
            if (words[i] == null) {
                continue;
            }
            for (int j = i + 1; j < words.length; j++) {
                if (words[j] == null) {
                    continue;
                }
                if (match(words[i], words[j])) {
                    result++;
                    words[j] = null;
                    break;
                }
            }
        }
        return result;
    }

    boolean match(String s1, String s2) {
        if (s1.length() != s2.length()) {
            return false;
        }
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) != s2.charAt(s2.length() - i - 1)) {
                return false;
            }
        }
        return true;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18