[Offoffoffoffer] Delete characters that appear in another string

Source: Internet
Author: User

 

The sword refers to the string-related questions on the offer.

Question: enter two strings to remove all the characters from the first string. For example, if you enter "They are students." and "aeiou", the first string after deletion becomes "Thy r stdnts .".

Here we mainly analyze two aspects:

1. Determine which characters are to be deleted. Like many string problems, you can open up a hash array, Initialize all to false, and set the ing position corresponding to the characters in the second string to true, the characters corresponding to these locations must be deleted from the first string.

2. For the delete character operation, delete one character each time, and then move the following elements to the left. As there may be multiple characters to be deleted, the time complexity of this method is O (n * n ). Here we have the O (n) deletion method. We can imagine that when a character needs to be deleted, we will fill the position occupied by it with the characters following it, this character is also deleted. In the specific implementation, we can define two pointers (pFast and pSlow), both of which point to the starting position of the first character at the beginning. When pFast points to a character that needs to be deleted, pFast is skipped and points to the next character. If the pFast points to a character that does not need to be deleted, assign the pFast to the character that pSlow points to, and move both pFast and pStart to the next character. In this way, the characters skipped by pFast are deleted. With this method, the entire deletion can be completed within O (n) time.

This deletion method is also used in the previous blog post that deletes repeated characters. For details, see http://blog.csdn.net/ns_code/article/details/41028151.

In addition, note that the char range is-128-127, and the unsigned char range is 0-255. Therefore, the ASCII value is between 128-255. If it is saved as a char type, the conversion range to int value is between-128--1, which is embodied in the following code.

The code written based on the above ideas is as follows:

 

# Include
 
  
# Include
  
   
# Define MAX 256 void DeleteChars (char * str1, char * str2) {if (str1 = NULL | str2 = NULL) return; bool hashtable [MAX]; memset (hashtable, 0, sizeof (hashtable); // set the position value in the hashtable array corresponding to the character in str2 to turewhile (* str2! = '\ 0') {// character with ASCII value between 128-255, // saved in char, converted to int type, int index between-128--1; if (* str2> = 0) index = * str2; elseindex = * str2 + 256; hashtable [index] = true; ++ str2;} char * pFast = str1; char * pSlow = str1; while (* pFast! = '\ 0') {int index; if (* pFast> = 0) index = * pFast; elseindex = * pFast + 256; // whether or not the characters to be deleted are met, pFast is moved back. // pSlow only moves back if (! Hashtable [index]) * pSlow ++ = * pFast; ++ pFast;} * pSlow = '\ 0';} int main () {char str1 [] = "They are students"; char str2 [] = "Tt"; DeleteChars (str1, str2); puts (str1); return 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.