See this interesting algorithmic question in Jane's book Today:
Thinking of solving problems
First define a variable element
, which defaults to the first character of the string. To element
determine whether a character is duplicated, as a datum element. Then go to define a variable count
to record the number of repetitions, the initial value should be 1
.
Then we go through the whole string, and we should 1
start with the characters in the number. If the current character equals element
, it means that the character appears in the current string and has been duplicated. Then count
it should be self-increasing.
If the current character is not equal, it means that the element
last character has been computed for successive repetitions and can be spliced. Next we can manipulate the current character and count
revert to the initial value of 1.
At the end of the traversal, the stitching operation is performed. Why not splice in the second step? Because the current character in the entire string repeats the number of times has not been counted, if the number of times to record a repetition, the operation of stitching, will certainly not achieve the desired results. If the string is “ppppp”
, if this is done, the output will be "p2p3p4p5"
. Therefore, it is necessary to count the number of repetitions of a character in order to perform splicing operations. We only need to count the number of consecutive repetitions of a character. When we traverse a character, we find that it is not equal, it means that element
the last character repetition has been statistically good, then we can do concatenation string operation.
Issues to be aware of
First general, after special. To control the boundary, we have to judge whether the string is null
or “”
, and then make the corresponding judgment.
The value of the variable that must be involved in the forward loop invariant every time.
For example, if a character a
appears in the string once 1
, it should be output instead a
a1
.
Do not use string stitching to stitch the data, String
is immutable objects, each splicing will generate a new String
object. Whether the use StringBuffer
is thread-safe, or a mutable object. Each call append()
does not generate a new string.
Code implementation:
Public classDemo { Public Static voidMain (string[] args) {Encode ("WWWBBAWABBB"); Encode (A); Encode ("ABCABCCCC"); Encode ("PPPPPRRRRPPP"); Encode (""); Encode (NULL); } Public Static voidencode (String str) {if(str = =NULL|| Str.equals ("") {System.out.println (""); return; } //accept the result using the StringBuffer variable stringStringBuffer SB =NewStringBuffer (); Charelement = Str.charat (0); intCount=1; for(intI=1;i<str.length (); i++) { if(element==Str.charat (i)) {Count++; }Else { //when you read another character that is not the same, the characters that are counted before the concatenationsb.append (Element); //If the statistic quantity is 1, the statistic quantity is not outputSb.append (count==1? "": Count); Element=Str.charat (i); Count=1; } } //ensure that when the last character and the number of statistics are stitched togethersb.append (Element); Sb.append (Count==1? "": Count); //output the entire resultSystem.out.println (sb.tostring ()); }}
Original address: Https://www.jianshu.com/p/32756f1ea4e7
Use the Java language to change consecutive occurrences of letters in a string to the "occurrences" + letters format