Skip to content

Commit

Permalink
create 2002 in c#
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammed785 committed Mar 4, 2023
1 parent 303ea5b commit 3450834
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
public class Solution {
public int MaxProduct(string s) {
if(s == null || s.Length < 2)
return 0;
if(s.Length == 2)
return 1;

int n = s.Length;
int total = 1 << n;

List<(int, int)> possible = new List<(int, int)>();

for(int i = 0; i < total; i++) {
StringBuilder sb = new StringBuilder();

for(int j = 0; j < n; j++) {
if((i & (1 << j)) != 0) {
sb.Append(s[j]);
}
}

if(IsPalindrome(sb.ToString())) {
possible.Add((i, sb.Length));
}
}

int ans = 0;
for(int i = 0; i < possible.Count; i++) {
int bitmask = possible[i].Item1;
int count = possible[i].Item2;
for(int j = i + 1; j < possible.Count; j++) {
int bitmask2 = possible[j].Item1;
int count2 = possible[j].Item2;
if((bitmask & bitmask2) == 0)
ans = Math.Max(ans, count * count2);
}
}
return ans;
}

private bool IsPalindrome(string s){
int i = 0;
int j = s.Length - 1;
while(i < j) {
if(s[i++] != s[j--])
return false;
}
return true;
}
}

0 comments on commit 3450834

Please sign in to comment.