Leetcode:strong Password Checker

Source: Internet
Author: User
Tags repetition uppercase letter strong password

 A Password is considered strong if   below Conditions is all met:it have at least  6 characters and at the most 20 characters. It must contain at least one lowercase letter, at least one uppercase letter, and at least one digit.  It must not contain three repeating characters in a row ( "..." is weak, but "..." aa...a  is strong, assuming and other conditions was met). Write a function Strongpasswordchecker (s), that takes a string s as input, and  return< /span> the MINIMUM change required to make s a strong password. If S is already strong, return  0 

Refer to Https://discuss.leetcode.com/topic/63854/o-n-java-solution-by-analyzing-changes-allowed-to-fix-each-problem

Http://www.cnblogs.com/grandyang/p/5988792.html

This problem gives us a password string, let us determine how many steps it needs to change can become a strong cipher string, and then given a strong password string condition, the length is between 6 to 20, must contain at least one lowercase letters, uppercase letters, numbers, and can not have three consecutive characters, gave us three methods of modification, Add a character, delete a character, or replace any character in any position, and let us change the minimum number of times to a strong cipher string. This problem is defined as hard really true, Bo Lord just look at the great God's posts have been watching for a long time, here is mainly reference to the great God Fun4leetcode post, personal feeling this is said to be very clear, here is copied over it. First of all, we look at the three main problems of the non-strong cipher string:

1. Length problem, when the length is less than 6, we want to add the length by inserting characters, when the length of more than 20 o'clock, we want to delete the characters.

2. Missing characters or numbers, when we are missing uppercase, lowercase, and numbers, we can complement them by inserting characters or substituting characters.

3. Repeat the character, this is also the biggest difficulty, because the insertion, deletion, or displacement can solve the problem of repeating characters, such as a string "AAAAA", we can use a permutation, such as replacing the middle character ' a ', or two times the insertion of characters, Insert a non-a character after the second A and fourth a, or you can remove 3 A to resolve the problem of repeating characters. Since the topic requires us to use the fewest steps, it is obvious that permutation is the most efficient way to repeat characters.

We can see by example that these three situations are not independent of each other, an operation can sometimes solve a number of problems, such as the string "AAA1A", we add a ' B ' after the second A, into the "AABA1A", which solves three problems, namely the increase in length, With the addition of the missing uppercase letters and the repetition, our goal is to find out as much as possible of the operations that solve many problems. Because of the situation three (repeating characters) can be solved with three kinds of operations, so we see separately can solve the situation one and the situation three, with simultaneous resolution of the situation two and the operation of the condition three. For simultaneous resolution one and two operation if the original cipher string length of less than 6 will overlap, so we have to discuss the situation:

When the length of the cipher string is less than 6 o'clock, the operation steps of condition one and condition two can completely cover the situation three, this is not difficult to understand, because in this case the number of repetitions of the range is [3,5], if there are three repeating characters, then the addition of three characters can solve the repeated character problem ("AAA", " A1BCAA "; If there are four repeating characters, the addition of two characters will also solve the repetition problem (" AAAA "," Aa1baa "), and if there are five repeating characters, then the increment and displace operations will also resolve the duplication problem (" AAAAA "," Aa1aab "). So we focus on the minimum number of steps to solve the situation at the same time two, first we calculate the current password string needs to fill a few characters to 6, the method of supplementary characters can only be used in the insertion character operation, and the insertion of the character operation may also solve the situation two, so when the number of missing species is less than equals diff, We don't have to add any more operations, and when diff doesn't completely cover the number of missing categories, we should also add the difference.

When the length of the password string is greater than or equal to 6, this situation is more complicated, because the length of the string can only exceed the standard is not possible, so we try not to use the insertion character operation, because it may make the length beyond the limit. Due to the length of uncertainty, so there may be a large number of repeating characters, then the resolution of the three becomes very important, because the previous analysis, the substitution of characters is the most efficient solution, but this method can not solve the situation one, because the length is exceeded, then how to replace the character, will not let the length of the reduction, But we can not have no brains to delete characters, this does not necessarily guarantee that the least steps, so in the case of the situation three should also consider the situation of a, here to use a trick (very much worship the big God can think out), for the repetition of the number of characters K is greater than or equal to 3, we do not directly delete it to 2 Instead of first deleting it to the nearest (3m+2), then if K is exactly divisible by 3, then we go directly to k-1, and if K is divided by 3 + 1, then it becomes k-2. The advantage of this is that 3m+2 repeats can be most efficiently removed by substituting m characters. So let's take a look at the specific steps, first we figure out more than 20 of the number over, we first add over to the result res, because in any case this over delete operation is to do. If not, over is 0, with the variable left representing the minimum number of replacements required to resolve a repeating character, initialized to 0. Then we iterate over the number of statistical characters before the array, if a character appears greater than or equal to 3, and at this time over more than 0, then we will reduce the number to the nearest 3m+2, over also corresponds to the reduction, note that once over is less than or equal to 0, do not delete the operation. If the number of repetitions is reduced to 3m+2, but over is still greater than 0, then we also need to further delete the operation, this time to delete the 3m each, until over is less than or equal to 0, the remaining if there are more than 3 repetitions of the number of characters, We figure out the number of replacement characters to be added directly to the left, and finally we compare left and missing, take the larger value added to the result res, see the code is as follows:

In addition, about 35-37 lines of explanation:

@sunvssoon Hi Sunvssoon. After we have transformed all repeating characters to (3m + 2) Form, if the total length is still greater than (i.e., O Ver_len > 0), then we had no choice but to start deleting characters to fix the repeating character problem.

Note now the total number of repeating characters at each position are arr[i], and to completely fix it only by deletion WI ll take at least (Arr[i] – 2) changes (example, to fix "AAAAA" by deletion, we need to delete at least 3 ' a ' s, which is th E total length minus 2). So "need" are the minimum number of changes needed to fix the repeating character problem by deletion only.

For the following expression, "arr[i"-= Over_len ", it's just a convenient-to-reset the number of repeating characters At this position after deletion. Strictly speaking we should distinguish the cases when the remaining deletions can totally fix the repeating problem O R they cannot. For the former we should reset "Arr[i", "to 2 when for the latter it should is" arr[i "-Over_len". But we can combine these. Cases into one expression "arr[i"-Over_len ", since for the former, as the problem can Tally fixed by deletion, we must has "Over_len >= arr[i"-2 ", which means" arr[i "-over_len" would be less than 3, so They'll no longer be considered as repeating character problem later.

As to the last expression, "over_len-= need", since fixing each repeating character problem would take at least "need" Del Etions, we need to reduce "over_len" by that amount to get the remaining available number of deletions.

1  Public classSolution {2      Public intStrongpasswordchecker (String s) {3         intres = 0, a = 1, a = 1, d = 1;4         Char[] Carr =S.tochararray ();5         int[] arr =New int[carr.length];6             7          for(inti = 0; I <arr.length;) {8             if(Character.islowercase (Carr[i])) A = 0;9             if(Character.isuppercase (Carr[i])) A = 0;Ten             if(Character.isdigit (Carr[i])) d = 0; One                  A             intj =i; -              while(I < carr.length && Carr[i] = = Carr[j]) i++; -ARR[J] = i-J; the         } -              -         intTotal_missing = (A + A +d); -      +         if(Arr.length < 6) { -Res + = total_missing + Math.max (0, 6-(Arr.length +total_missing)); /priority Insert missing element with replace after full 6 , +                                              A}Else { at             intOver_len = Math.max (arr.length-20, 0), Left_over = 0; -Res + =Over_len; -                  -              for(intK = 1; K < 3; k++{//Repeat all three or more elements of delete into 3m+2, -                  for(inti = 0; I < arr.length && over_len > 0; i++) { -                     if(Arr[i] < 3 | | arr[i]% 3! = (k-1))ContinueWhen the remainder is 0, the remainder is 1, 2 is skip, and the remainder is 1, the remainder is 0, and 2 is skip inArr[i]-=Math.min (Over_len, k);//The remainder is 1 minus two; the remainder is 0, minus one -Over_len-=K; to                 } +             } -                  the              for(inti = 0; i < arr.length; i++) {//Analysis Over_len If >0 is still in progress *                 if(Arr[i] >= 3 && over_len > 0) { $                     intNeed = Arr[i]-2The amount that the target wants to be subtracted, however over_len not necessarily have so muchPanax NotoginsengArr[i]-=Over_len;//simple writing, in fact, after the arr[i]==2 (over_len>=need, only reduce need so much) and Arr[i]==arr[i]-over_len (over_len< Need) Two cases -Over_len-=need; the                 } +                      A                 if(Arr[i] >= 3) Left_over + + Arr[i]/3,//This section needs to use replace to eliminate residual duplication the             } +                  -Res + =Math.max (total_missing, left_over);//replace can simultaneously solve the missing element situation $         } $              -         returnRes; -     } the}

Leetcode:strong Password Checker

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.