Algorithm Analysis-delete characters with the least number of occurrences in a string

Source: Internet
Author: User

Compile a function to delete the characters with the least number of occurrences in a string. If the number of occurrences of multiple characters is the same, delete them. Output the string after the words are deleted. Other characters in the string are in the original order. Format description: A string contains only letters, and cannot be an empty string. It does not contain spaces. A word can only contain lowercase English letters. The length of the input string cannot exceed 20 char characters.


Algorithm ideas:

(1) define an integer array with a length of 26

The subscript represents the 26 lowercase letters a-z from 0 to 25, respectively, and is used to save the number of occurrences of each character in the string.

(Because 'a'-'A' = 0, 'z'-'A' = 25 corresponds to the subscript of the array)

Shows the number of occurrences of "abcdddeeffffxxyz" characters:


(2) obtain the minimum value in the array, that is, the minimum number of times the characters in the string appear in min.(Find the first non-zero value in the array, compare the size from the front to the back, and find the minimum value ).

(3) define an empty character array to save the New String(The characters that appear more than min are stored in an empty array in sequence, and the string ending sign '\ 0' is added at the end ').

(4) The algorithm ends.

# Include <stdio. h> # include <string. h> # include <malloc. h> char * change (char * str) {int alpha [26] = {0}; // defines and initializes a 26-character array, used to save the number of times each lowercase letter appears int len = strlen (str); // String Length for (int I = 0; I <len; I ++) {// traverses the character int index = str [I]-'A' in the string; // The subscript alpha [index] ++ of the character in the alpha array; // times + 1} // After the code above, the number of occurrences of each lowercase letter has been saved in the alpha array int min = 0; for (I = 0; I <len; I ++) {// obtain the first non-zero value in the array if (alpha [I]! = 0) min = alpha [I] ;}for (I = 0; I <26; I ++) {// obtain the minimum element in the alpha array, that is, the minimum number of times that lowercase letters appear. if (alpha [I]! = 0 & alpha [I] <min) {min = alpha [I] ;}} char * newStr = (char *) malloc (sizeof (char) * len ); // used to save the changed string char * p = str; int num = 0; while (* p) {// traverses the string, save the string that appears more than min to newStr int index2 = * p-'A'; if (alpha [index2]! = Min) {newStr [num] = * p; num ++;} p ++;} newStr [num] = '\ 0'; return newStr;} void main () {char str [100]; // Save the input string gets (str); // input the string char * res = change (str); puts (res) from the keyboard ); free (res );}




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.