5373 {
5374 const int n = password.length();
5375 bool has_lower = false;
5376 bool has_upper = false;
5377 bool has_digit = false;
5378 for(const char ch: password) {
5379 if(islower(ch) != 0) {
5380 has_lower = true;
5381 } else if(isupper(ch) != 0) {
5382 has_upper = true;
5383 } else if(isdigit(ch) != 0) {
5384 has_digit = true;
5385 }
5386 }
5387 const int categories = static_cast<int>(has_lower) + static_cast<int>(has_upper) + static_cast<int>(has_digit);
5388
5389 if(n < 6) {
5390 return max(6 - n, 3 - categories);
5391 }
5392 if(n <= 20) {
5393 int replace = 0;
5394 int count = 0;
5395 char current = '#';
5396
5397 for(const char ch: password) {
5398 if(ch == current) {
5399 ++count;
5400 } else {
5401 replace += count / 3;
5402 count = 1;
5403 current = ch;
5404 }
5405 }
5406 replace += count / 3;
5407 return max(replace, 3 - categories);
5408 }
5409
5410 int replace = 0;
5412
5413 int rm2 = 0;
5414 int count = 0;
5415 char cur = '#';
5416
5417 for(const char ch: password) {
5418 if(ch == cur) {
5419 ++count;
5420 } else {
5421 if(remove > 0 && count >= 3) {
5422 if(count % 3 == 0) {
5423
5425 --replace;
5426 } else if(count % 3 == 1) {
5427
5428 ++rm2;
5429 }
5430
5431 }
5432 replace += count / 3;
5433 count = 1;
5434 cur = ch;
5435 }
5436 }
5437 if(remove > 0 && count >= 3) {
5438 if(count % 3 == 0) {
5440 --replace;
5441 } else if(count % 3 == 1) {
5442 ++rm2;
5443 }
5444 }
5445 replace += count / 3;
5446
5447
5448 const int use2 = min({replace, rm2,
remove / 2});
5449 replace -= use2;
5451
5452 const int use3 = min(replace, remove / 3);
5453 replace -= use3;
5455 return n - 20 + max(replace, 3 - categories);
5456 }
void remove(TreeNode *&root, int x)