int arrLen = strs.size(); int ans = -1; for (int i = 0; i < arrLen; i++) { int signedSizeI = int(strs[i].size()); if (ans > signedSizeI) continue; int j = 0; for (; j < arrLen; j++) { if (j >= arrLen) break; if (i == j) continue; int signedSizeJ = int(strs[j].size()); int permission = signedSizeI > signedSizeJ; if (! permission && this->isSub(strs[i], strs[j])) { break; } } if (j >= arrLen && ans < signedSizeI) { ans = signedSizeI; } } return ans; }
private: intisSub(const string& a, const string& b){ int lengthA = a.length(), lengthB = b.length(); int i = 0, j = 0; while(i < lengthA && j < lengthB) { if (a[i] == b[j++]) { i++; } } return i == lengthA; } };
int arrLen = strs.size(); int ans = -1; for (int i = 0; i < arrLen; i++) { int signedSizeI = int(strs[i].size()); if (ans > signedSizeI) continue; int j = 0; for (; j < arrLen; j++) { if (j >= arrLen) break; if (i == j) continue; int signedSizeJ = int(strs[j].size()); int permission = signedSizeI > signedSizeJ; if (! permission && this->isSub(strs[i], strs[j])) { break; } elseif (permission) { j = arrLen - 1; } } if (j >= arrLen && ans < signedSizeI) { ans = signedSizeI; } } return ans; }
set<int> invalid; int arrLen = strs.size(); int ans = -1; for (int i = arrLen - 1; i >= 0; i--) { if (invalid.count(i) == 1) continue; int signedSizeI = int(strs[i].size()); if (ans > signedSizeI) continue; int j = i - 1; for (; j >= 0; j--) { int signedSizeJ = int(strs[j].size()); if (j < 0) break; if (i == j) continue; if (this->isSub(strs[i], strs[j])) { if(invalid.count(i) == 0) invalid.insert(i); if (signedSizeI == signedSizeJ) { if (this->isSub(strs[j], strs[i])){ invalid.insert(j); } } else { break; } } } if (invalid.count(i) == 0 && j < 0 && ans < signedSizeI) { ans = signedSizeI; } } return ans; }
private: intisSub(const string& a, const string& b){ int lengthA = a.length(), lengthB = b.length(); int i = 0, j = 0; while(i < lengthA && j < lengthB) { if (a[i] == b[j++]) { i++; } } return i == lengthA; } };