Huawei machine exercise questions-compressing strings

Source: Internet
Author: User

Question:

Enter a string of lowercase letters (~ Z. Compile a character string compression program to compress duplicate letters that are present continuously in the string and output the compressed character string.

Compression rules:
1. Only compress the repeated characters. For example, the compressed string "abcbc" is still "abcbc" because there are no consecutive duplicate characters ".
2. The format of the compressed field is "repeated characters + characters ". For example, after the string "xxxyyyyz" is compressed, it becomes "3x6yz ".

Required implementation functions:
Void stringZip (const char * pInputStr, long lInputLen, char * pOutputStr );

[Input] pInputStr: input string
LInputLen: length of the input string
[Output] pOutputStr: Output string. The space has been opened up and the length of the input string is equal to that of the input string;

[Note] You only need to complete the function algorithm, and there is no IO input or output in the middle.

Example
Input: "cccddecc" output: "3c2de2c"
Input: "adef" output: "adef"
Input: "pppppppp" output: "8 P"


Analysis: character compression is used to determine the number of identical characters, and then the characters are prefixed with the number, so we can count the characters and splice them again.

Here I come up with two methods,

1: create two lists, one for storing characters, and the other for storing the number of characters. Then, recombine the strings based on the two lists;

2: directly combine strings in the counting process

Here I chose the second approach. I felt that I could write less code and I was too lazy!


The Code is as follows:

Package com. wenj. test;

/*
* @ Author wenj91-PC
*
*/

Public class TestStringZip {

Public static void main (String args []) {
String strIn = "pppppppp ";
TestStringZip ts = new TestStringZip ();
System. out. println (ts. stringZip (strIn ));
}

Public String stringZip (String strIn ){
String strTemp = strIn;
Char [] strC = strTemp. toCharArray ();

String temp = ""; // StringBuffer is suitable for use here, but it is easy for readers to pay attention to it.
Int count = 0;
Char preChar = strC [0];
For (int I = 0; I <strC. length; I ++ ){
Char cTemp = strC [I];
If (cTemp = preChar) {// determine whether the character is the same as the previous one
Count ++;
} Else {
If (count> 1 ){
Temp + = count;
}
Count = 1;
Temp + = preChar;
PreChar = strC [I];
}
If (I = strC. length-1) {// determine whether the end is reached
If (count> 1 ){
Temp + = count;
}
Temp + = strC [I];
}
}

Return temp;
}
}

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.