Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 37f302d

Browse files
committed
Have implemented the code to find the maximum numbers of string pairs.
1 parent a40ba0c commit 37f302d

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

.idea/workspace.xml

Lines changed: 18 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/com/raj/FindMaximumNumberOfStringPairs.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,41 @@
4141
package com.raj;
4242

4343
public class FindMaximumNumberOfStringPairs {
44+
public static void main(String[] args) {
45+
// Initialization.
46+
String[] words = {"cd", "ac", "dc", "ca", "zz"};
47+
int count = 0;
4448

49+
// Logic.
50+
outerLoop:
51+
for (int i = 0; i < words.length - 1; i++) {
52+
String reverseString = getReverseString(words[i]);
53+
for (int j = i + 1; j < words.length; j++) {
54+
if (reverseString.equals(words[j])) {
55+
count++;
56+
continue outerLoop;
57+
}
58+
}
59+
}
60+
61+
// Display the result.
62+
System.out.println("The maximum numbers of string pairs are: " + count);
63+
}
64+
65+
private static String getReverseString(String s) {
66+
StringBuilder str = new StringBuilder(s);
67+
68+
int start = 0;
69+
int end = str.length() - 1;
70+
71+
while (start < end) {
72+
char tmp = str.charAt(start);
73+
str.setCharAt(start, str.charAt(end));
74+
str.setCharAt(end, tmp);
75+
start++;
76+
end--;
77+
}
78+
79+
return str.toString();
80+
}
4581
}

0 commit comments

Comments
 (0)