Determines whether a string can form a background by changing the character position.

Source: Internet
Author: User

The solution is to determine whether there are any odd characters in the string. If the number of such characters is greater than 1, it means that it cannot form a background; if it is less than or equal to 1, it can form a background.

Strlen () is used, and put into the for loop can easily lead to lower program running efficiency. For example, when the input string is large, it is 10 ^ 5 characters long.

For (INT I = 0; I <strlen (string); I ++) causes the program to run slowly. Switch:

Int length = strlen (string); For (INT I = 0; I <length; I ++) can greatly improve efficiency.

 

#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>void findPalind(char *arr){        int flag = 0;    // Find the required answer here. Print Yes or No at the end of this function depending on your inspection of the string        int result[26];        for (int i=0; i<26; i++) {        result[i] = 0;    }        int lenth = (int)strlen(arr);    for (int i=0; i<lenth; i++) {        int index = (int)*(arr + i) - (int)‘a‘;        if (result[index] == 0) {            result[index] = 1;        } else {            result[index] = 0;        }    }        int sum = 0;    for (int i=0; i<26; i++) {        sum += result[i];    }        if (sum >= 2) {        flag = 1;    } else {        flag = 0;    }        if (flag==0)        printf("YES\n");    else        printf("NO\n");        return;}int main() {        char arr[100001];    scanf("%s",arr);    findPalind(arr);    return 0;}
Code

 

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.