Classic algorithm interview Topics---1.3

Source: Internet
Author: User

Topic

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

Follow up

Write The test cases for this method.

The algorithm is designed and the code is written out to remove repeated characters from the string, and additional cache space cannot be used. Note: You can use an additional one or two variables, but do not allow an extra copy of the array to be opened.

Further,

Write test cases for your program.

Answer

This title is actually for you to remove the repeating characters from the string in the place. You can ask the interviewer if an additional copy of the array is not allowed to open an array at all, or to open a fixed size array unrelated to the size of the problem (that is, the length of the string).

If you are not allowed to open an array at all, you can only use an additional one to two variables. Then, you can access each element of the array in turn, and each time you access one, remove the same element from the element at the end of the string (for example, ' \0′ '). The complexity is O (n2) and the code is as follows:

void Removeduplicate (char s[]) {int Len= strlen (s);if(Len<2)return;//Only one character, certainly no duplicates    intp =0;//corresponds to a cursor    //Starting from 0 to find characters that are not duplicated     for(intI=0; I <Len; ++i) {if(S[i]! =' + ') {s[p++] = s[i]; for(intJ=i+1; J <Len; ++J)if(S[j]==s[i]) s[j] =' + '; }} S[p] =' + ';}

If you can open a fixed size, and the problem size (that is, string length) independent of the array, then you can use an array to characterize the occurrence of each character (assuming that the ASCII character, the array size is 256), so that only need to traverse through the string, time complexity O (n). The code is as follows:

voidRemoveduplicate (CharS[]) {intLen =strlen(s);if(Len <2)return;BOOLc[ the];//This uses true to identify that the character has appeared. Save time compared to the last.     memsetC0,sizeof(c));intp =0; for(intI=0; i < Len; ++i) {if(!c[s[i]])            {s[p++] = s[i]; C[s[i]] =true; }} S[p] =' + '; }

If the character set is smaller, for example a A-Z, where the string contains only lowercase letters, each bit of an int variable is used to characterize each character, as well as to remove the repeating character in the O (n) time, and no additional array is required. The code is as follows:

void Removeduplicate (CharS[]) {intLen = strlen (s);if(Len <2)return;int Check=0, p =0; for(intI=0; i < Len; ++i) {the//int variable itself accounts for 4 bytes in memory, which is 32 bits!!         intv = (int) (s[i]-' A ');//If there are no duplicate letters (less than 32 letters), it will not appear (check & (1 << v) ==1!         if((Check& (1<< v) = =0) {s[p++] = s[i];Check|= (1<< v); }} S[p] =' + ';}

Test Case:

A string that does not contain repeating characters, such as: ABCD
The string is full of repeating characters, for example: AAAA
Empty string
Repeating characters appear continuously, for example: AAABBB
Repeated characters do not appear consecutively, for example: Abababa
The complete code is as follows:

#include <iostream>#include <cstring>using namespace STD;stringRemoveDuplicate1 (strings) {intCheck =0;intLen = S.length ();if(Len <2)returnSstringstr =""; for(intI=0; i<len; ++i) {intv = (int) (s[i]-' A ');if(Check & (1&LT;&LT;V)) = =0) {str + = s[i]; Check |= (1&LT;&LT;V); }    }returnSTR;}stringRemoveDuplicate2 (strings) {intLen = S.length ();if(Len <2)returnSstringstr =""; for(intI=0; i<len; ++i) {if(S[i]! =' + ') {str + = s[i]; for(intj=i+1; j<len; ++J)if(S[j]==s[i]) s[j] =' + '; }    }returnSTR;}voidRemoveDuplicate3 (CharS[]) {intLen =strlen(s);if(Len <2)return;intp =0; for(intI=0; i<len; ++i) {if(S[i]! =' + ') {s[p++] = s[i]; for(intj=i+1; j<len; ++J)if(S[j]==s[i]) s[j] =' + '; }} S[p] =' + ';}voidRemoveDuplicate4 (CharS[]) {intLen =strlen(s);if(Len <2)return;BOOLc[ the];memsetC0,sizeof(c));intp =0; for(intI=0; i<len; ++i) {if(!c[s[i]])            {s[p++] = s[i]; C[s[i]] =true; }} S[p] =' + '; }voidRemoveDuplicate5 (CharS[]) {intLen =strlen(s);if(Len <2)return;intCheck =0, p =0; for(intI=0; i<len; ++i) {intv = (int) (s[i]-' A ');if(Check & (1&LT;&LT;V)) = =0) {s[p++] = s[i]; Check |= (1&LT;&LT;V); }} S[p] =' + ';}intMain () {stringS1 ="ABCDE";stringS2 ="AAABBB";stringS3 ="";stringS4 ="ABABABC";stringS5 ="CCCCC";cout<<removeduplicate1 (S1) <<" "<<removeduplicate2 (S1) <<endl;cout<<removeduplicate1 (S2) <<" "<<removeduplicate2 (S2) <<endl;cout<<removeduplicate1 (S3) <<" "<<removeduplicate2 (S3) <<endl;cout<<removeduplicate1 (S4) <<" "<<removeduplicate2 (S4) <<endl;cout<<removeduplicate1 (S5) <<" "<<removeduplicate2 (S5) <<endl;CharSs1[] ="ABCDE";CharSs2[] ="AAABBB";CharSs3[] ="";CharSs4[] ="ABABABC";CharSs5[] ="CCCCC";    RemoveDuplicate5 (SS1);    RemoveDuplicate5 (SS2);    RemoveDuplicate5 (SS3);    RemoveDuplicate5 (SS4); RemoveDuplicate5 (SS5);cout<<ss1<<" "<<ss2<<" "<<ss3<<" "<<ss4<<" "<<ss5<<endl;return 0;}

The test results are as follows:

Classic algorithm interview Topics---1.3

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.