2024年1月17日每日一题--2744. 最大字符串配对数目

2744. 最大字符串配对数目

题目分析

仔细思考了一下,只想出来一种时间复杂度为 n的平方 方法

方案一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public int maximumNumberOfStringPairs(String[] words) {
int result = 0;
for (int i = 0; i < words.length; i++) {
for (int j = i + 1; j < words.length; j++) {
if (isTrue(words[i], words[j])) {
result++;
}
}
}
return result;
}

public boolean isTrue(String str1, String str2) {
if (str1.length() != str2.length()) return false;
int length = str1.length();

for (int i = 0; i < length; i++) {
if (str1.charAt(i) != str2.charAt(length - i - 1)) return false;
}
return true;
}
}

结果

分析

时间复杂度:
O( n ^ 2)

空间复杂度:
O( 1 )

方案二

1

官方题解

1


2024年1月17日每日一题--2744. 最大字符串配对数目
http://yuanql.top/2024/01/17/02_02_leetcode_每日一题/2024年1月17日每日一题--2744. 最大字符串配对数目/
作者
Qingli Yuan
发布于
2024年1月17日
许可协议