最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Leetcode 2744. Find Maximum Number of String Pairs

2023-06-25 12:23 作者:您是打尖兒還是住店呢  | 我要投稿

You are given a?0-indexed?array?words?consisting 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

  • words?consists 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的評論 (共 條)

分享到微博請遵守國家法律
吉林市| 油尖旺区| 霍林郭勒市| 刚察县| 济源市| 蕉岭县| 灵武市| 邵阳市| 巨鹿县| 长顺县| 宜宾县| 光山县| 湖州市| 马尔康县| 周口市| 新巴尔虎左旗| 柳林县| 遂昌县| 辽阳市| 延吉市| 光泽县| 铁力市| 武山县| 临城县| 汶上县| 榆社县| 合阳县| 苍南县| 白银市| 容城县| 彩票| 麻栗坡县| 齐齐哈尔市| 弥渡县| 兰西县| 鄂托克旗| 仪陇县| 青州市| 云梦县| 凤庆县| 丁青县|