首页 > 公益

Leetcode 2744. Find Maximum Number of String Pairs

来源:哔哩哔哩 时间:2023-06-25 13:45:38

You are given a 0-indexed array wordsconsisting of distinct strings.

The string words[i]can be paired with the string words[j]if:


(资料图片仅供参考)

The string words[i]is equal to the reversed string of words[j].

0 <= i < j < words.length.

Return the maximum number of pairs that can be formed from the array words.

Note that each string can belong in at most one pair.

Example 1:

Input: words = ["cd","ac","dc","ca","zz"]Output: 2Explanation: In this example, we can form 2 pair of strings in the following way:- We pair the 0th string with the 2nd string, as the reversed string of word[0] is "dc" and is equal to words[2].- We pair the 1st string with the 3rd string, as the reversed string of word[1] is "ca" and is equal to words[3].It can be proven that 2 is the maximum number of pairs that can be formed.

Example 2:

Input: words = ["ab","ba","cc"]Output: 1Explanation: In this example, we can form 1 pair of strings in the following way:- We pair the 0th string with the 1st string, as the reversed string of words[1] is "ab" and is equal to words[0].It can be proven that 1 is the maximum number of pairs that can be formed.

Example 3:

Input: words = ["aa","ab"]Output: 0Explanation: In this example, we are unable to form any pair of strings.

Constraints:

1 <= words.length <= 50

words[i].length == 2

wordsconsists of distinct strings.

words[i]contains only lowercase English letters.

找是否是反序的字符串对,用stringbuilder即可处理,

下面是代码:

import java.util.Arrays;

public class Code2744 {

public static void main(String[] args) {

}

public static int maximumNumberofStringPairs(String[]words){

boolean[]bn=new boolean[words.length];

int cnt=0;

Arrays.fill(bn, false);

for (int i = 0; i < words.length-1; i++) {

for (int j = i+1; j < words.length; j++) {

if(check(words[i], words[j])&&bn[i]==false&&bn[j]==false){

bn[i]=true;

bn[j]=true;

cnt++;

}

}

}

return cnt;

}

public static boolean check(String a, String b){

StringBuilder sb=new StringBuilder(a);

if(sb.reverse().toString().equals(b)){

return true;

}else{

return false;

}

}

}

Runtime: 8 ms, faster than 33.33% of Java online submissions for Find Maximum Number of String Pairs.

Memory Usage: 43.9 MB, less than 33.33% of Java online submissions for Find Maximum Number of String Pairs.

相关稿件

Leetcode 2744. Find Maximum Number of String Pairs

全球热推荐:IPO视频 | 长风药业IPO 负债率低于同行均值

5年期LPR为何只降10个基点 焦点信息

天天资讯:文保“活”起来 城市“火”起来

生死关头,农民工师徒李羿、张勇明纵身跳入江中......

热文:鹅湖书院景区:文明旅游 一路好风光

环球滚动:俄媒:普里戈任与瓦格纳集团武装人员已全部从俄南部军区司令部撤离

白鹤滩-浙江特高压工程输电能力达800万千瓦 环球速递

三峡船闸通航20年累计货运量19.1亿吨

端午期间,港股风电板块持续走低!

高容量锂离子电池新型正极材料技术获突破 天天观速讯

全球报道:记者直击丨莫斯科红场附近街区夜间行人较往常明显减少

气象专家:厄尔尼诺将给我国带来哪些影响?

制作镜像系统u盘安装系统-(制作镜像系统u盘安装系统教程)-每日讯息

综合消息:普京向多国通报俄国内局势 俄国防部呼吁瓦格纳组织成员勿参加叛乱|视讯

焦点滚动:北方高温稍稍减弱 9省市仍有35℃以上高温

英特尔N100处理器性能曝光 跑分达i5-7400水平

大众汽车重组各品牌并设置独立绩效 2030年销售回报率提升至9%-11%-天天日报

深蓝S7将于6月25日上市,预售价16.99万起,轴距超Model Y 环球快消息

我国储粮新技术处于国际领先水平 “智慧粮库”让小麦“冻龄” 环球观察

预计年内上市 奇瑞TJ-1官方定名探索06

不只是苹果 iPhone,英国称安卓手机也存在 SOS 误报问题

华为P60,近期好价,喜欢摄影的用户切勿错过 前沿热点

长今之梦第二季第三十五集金刚山的老虎_长今之梦第二季

环球速看:罄竹难书系列作者_罄竹难书系列

智能大棚

国际排联通过2028年洛杉矶奥运会参赛席位分配方式

要闻速递:河南高考分数线公布:本科一批文科547分、理科514分

普里戈任称接受瓦格纳停止在俄领土上行动的提议 天天热议

WTA伯明翰赛止步女单4强 朱琳草地赛季再创佳绩|动态