[CareerCup] Arrays and Strings-Q1.3

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/ns_code/article/details/21328151


Question:

Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not.

FOLLOW UP

Write the test cases for this method.

Translation:

Remove repeated characters in the string. For example, if abcadc is changed to abcd after being removed, you can define one or two additional variables, but you cannot open an additional array. Write test cases for the algorithm.

Ideas:

This topic mainly limits the space complexity. Without this requirement, we can have a very good solution (assuming the letters in the string are all lowercase letters ):

For example, when the string is very long, we can use the same idea as in the Q1.1 question to open a bool array with a size of 26, the initial value is false, traverse the string, the first time we encounter a character, set the element at the corresponding position of the character in the bool array to true. If it is encountered again, it indicates that a duplicate character exists and you can remove it.

For example, if the string is not very long, we can consider using the prefix array in the KMP algorithm, create an array that is 1 longer than the string length (the prefix value is also required for the last character '\ 0') as the string prefix array, if the value of next [I]> 0 in the prefix array, it indicates that the characters at the next [I] position in front of the position are repeated with the previous characters. You can remove these characters.

However, the space complexity is O (1), so we can only use the simplest Traversal method to first compare the first character with the subsequent characters, if there is a duplicate, replace the duplicate characters with '\ 0', and compare the second character with the character on the right. If there is a duplicate, replace the repeated characters with '\ 0', so that the loop ends until the last character, of course, at the same time when' \ 0' is encountered (the position where the repeated characters appear, has been filled with '\ 0'), move the subsequent characters to the front, replace' \ 0' with '\ 0', and set the last position after the shift to' \ 0 ', indicates the end of the string. The time complexity is O (n * n ).

For test cases, consider the following situations:

Char str1 [] = "abcdfbdk"; // random string
Char str2 [] = "ababababab"; // repeated characters Alternate
Char str3 [] = "aaaabbbb"; // repeated characters appear continuously
Char str4 [] = "aaaaaaaa"; // All are repeated characters
Char str5 [] = "abcdefgh"; // No repeated characters
Char str6 [] = ""; // empty string

Implementation Code:

/*************************************** * ******* Title Description: remove repeated characters in the string. For example, if abcadc is changed to abcd after being removed, you can define one or two additional variables. However, you cannot open an additional array and write the test case Date for this algorithm: *************************************** * ********/# include <stdio. h> # include <string. h> void remove (char * str) {int len = strlen (str); if (len <2) return; int I, j; int p = 0; // The initial values of p are equal to those of I. They are all 0for (I = 0; I <len; I ++) {// if this character is not '\ 0 ', compared with the following characters, if (str [I]) {// if the current character is not '\ 0', p is always equal to I, // if the current character is '\ 0', p is smaller than I, then, use the following characters to fill in str [p ++] = str [I] In front of '\ 0'; // compare each character with the character following it, // If repeated characters exist, use '\ 0' to replace the subsequent repeated characters for (j = I + 1; j <len; j ++) if (str [I] = str [j]) str [j] = '\ 0';} str [p] =' \ 0';} int main () {char str1 [] = "abcdfbdk"; // random string char str2 [] = "abababab"; // repeated characters alternate with char str3 [] = "aaaabbbb "; // repeated characters appear consecutively char str4 [] = "aaaaaaaa"; // All repeated characters char str5 [] = "abcdefgh "; // The char str6 [] = ""; // empty string remove (str1); remove (str2); remove (str3); remove (str4 ); remove (str5); remove (str6); puts (str1); puts (str2); puts (str3); puts (str4); puts (str5); puts (str6 ); return 0 ;}

Next we will provide a method to open up a fixed-size bool array, that is, the first method in our analysis. The implementation code is as follows:

/* Method for opening up a bool array with a length of 26 */void remove1 (char * str) {int len = strlen (str); if (len <2) return; int I; int p = 0; bool A [MAX]; memset (A, 0, sizeof (A); for (I = 0; I <len; I ++) {int index = str [I]-'A'; if (! A [index]) {str [p ++] = str [I]; A [index] = true ;}} str [p] = '\ 0 ';}

Test results:



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.