-
Notifications
You must be signed in to change notification settings - Fork 0
/
2744_Find_Maximum_Number_Of_String_Pairs.c
55 lines (47 loc) · 2.03 KB
/
2744_Find_Maximum_Number_Of_String_Pairs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// 2744. Find Maximum Number of String Pairs
/* 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].
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: 2
Explanation: 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: 1
Explanation: 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: 0
Explanation: In this example, we are unable to form any pair of strings.
Constraints:
1. 0 <= i < j < words.length.
2. 1 <= words.length <= 50
3. words[i].length == 2
4. words consists of distinct strings.
5. words[i] contains only lowercase English letters.
*/
/*
=> Break statement to deal with palindromic strings (e.g. "zz")
=> Iteration to go from 1st string to Last string
=> In another iteration (From current string to last string), check match with other reversed strings. If match => Count++
*/
int maximumNumberOfStringPairs(char ** words, int wordsSize){
int count = 0;
for(int i = 0; i<wordsSize; i++){
for(int j = 0; j < wordsSize; j++){
if(words[i] == words[j])
break;
if((words[i][0] == words[j][1]) && (words[i][1] == words[j][0]))
count++;
}
}
return count;
}