第一题
class Solution {
public String reversePrefix(String word, char ch) {
int index = word.indexOf(ch + "");
if (index != -1) {
String s = word.substring(0, index+1);
StringBuilder stringBuilder = new StringBuilder(s);
return stringBuilder.reverse().toString()+word.substring(index+1);
}
return word;
}
}
第二题
class Solution {
public long interchangeableRectangles(int[][] r) {
int n = r.length;
double[] rate = new double[n];
for (int i = 0; i < n; i++) {
rate[i] = (double) r[i][0] / (double) r[i][1];
}
Arrays.sort(rate);
long res = 0;
for (int i = 0; i < n; i++) {
int j = i + 1;
while (j < n && rate[i] == rate[j]) {
j++;
}
long len = j - i;
res += (len * (len - 1)) / 2;
i = j-1;
}
return res;
}
}
第三题
class Solution {
public:
int ans = 0, n;
string S;
bool check(string v){
int m = v.size();
for(int i=0; i<m; i++){
if(v[i] != v[m-1-i]) return false;
}
return true;
}
void dfs(int u, string l, string r){
if(u == n){
int t = l.size() * r.size();
if(t <= ans) return;
if(check(l) && check(r)){
ans = max(ans, t);
}
return;
}
dfs(u+1, l, r);
dfs(u+1,l+S[u], r);
dfs(u+1, l, r+S[u]);
}
int maxProduct(string s) {
n = s.size();
S = s;
dfs(0, "", "");
return ans;
}
};