Skip to content

Commit 3450834

Browse files
committed
create 2002 in c#
1 parent 303ea5b commit 3450834

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
public class Solution {
2+
public int MaxProduct(string s) {
3+
if(s == null || s.Length < 2)
4+
return 0;
5+
if(s.Length == 2)
6+
return 1;
7+
8+
int n = s.Length;
9+
int total = 1 << n;
10+
11+
List<(int, int)> possible = new List<(int, int)>();
12+
13+
for(int i = 0; i < total; i++) {
14+
StringBuilder sb = new StringBuilder();
15+
16+
for(int j = 0; j < n; j++) {
17+
if((i & (1 << j)) != 0) {
18+
sb.Append(s[j]);
19+
}
20+
}
21+
22+
if(IsPalindrome(sb.ToString())) {
23+
possible.Add((i, sb.Length));
24+
}
25+
}
26+
27+
int ans = 0;
28+
for(int i = 0; i < possible.Count; i++) {
29+
int bitmask = possible[i].Item1;
30+
int count = possible[i].Item2;
31+
for(int j = i + 1; j < possible.Count; j++) {
32+
int bitmask2 = possible[j].Item1;
33+
int count2 = possible[j].Item2;
34+
if((bitmask & bitmask2) == 0)
35+
ans = Math.Max(ans, count * count2);
36+
}
37+
}
38+
return ans;
39+
}
40+
41+
private bool IsPalindrome(string s){
42+
int i = 0;
43+
int j = s.Length - 1;
44+
while(i < j) {
45+
if(s[i++] != s[j--])
46+
return false;
47+
}
48+
return true;
49+
}
50+
}

0 commit comments

Comments
 (0)