Day33~实现一个算法来识别一个字符串。

实现一个算法来识别一个字符串str2是另一个字符串str1的排列。排列的解释如下:如果将str1的字符拆分开,重新排列后再拼接起来,能够得到str2,那么就说字符串str2是字符串str1的排列。(不忽略大小写)如果str2字符串是str1字符串的排列,则输出 YES;如果str2字符串不是str1字符串的排列,则输出NO。

cpp 复制代码
#include <stdio.h>
#include <string.h>

#define MAX_SIZE 100

void judge_str(char *str1, char *str2)
{
    int str1_len = strlen(str1);
    int str2_len = strlen(str2);

    // 将str1进行排序
    for (int i = 0; i < str1_len; i++)
    {
        for (int j = 0; j < str1_len - 1 - i; j++)
        {
            if (str1[j] > str1[j + 1])
            {
                int temp = str1[j];
                str1[j] = str1[j + 1];
                str1[j + 1] = temp;
            }
        }
    }

    // 将str2进行排序
    for (int i = 0; i < str2_len; i++)
    {
        for (int j = 0; j < str2_len - 1 - i; j++)
        {
            if (str2[j] > str2[j + 1])
            {
                int temp = str2[j];
                str2[j] = str2[j + 1];
                str2[j + 1] = temp;
            }
        }
    }

    // 将str1和str2进行比较
    if (strcmp(str1, str2) == 0)
    {
        printf("YES\n");
    }
    else
    {
        printf("NO\n");
    }
}

int main()
{
    char str1[MAX_SIZE] = {0};
    char str2[MAX_SIZE] = {0};

    scanf("%s", str1);
    scanf("%s", str2);

    judge_str(str1, str2);

    return 0;
}
相关推荐
Dola_Zou7 小时前
工厂智能排产软件算法保护与按产线计费实战
算法·自动化·软件工程·软件加密
2601_962300478 小时前
halconpython联合开发网_C#与Halcon联合开发实例,并做了可移动的ROI库
图像处理·c·halcon·软件开发·roi
啦啦啦啦啦zzzz8 小时前
Logger的组装(c++20)
c++·算法·c++20
2601_962097369 小时前
1. 使用 C 或 C++ 扩展 Python
python·api·c·引用计数·扩展模块
尾善爱看海9 小时前
前端算法与手写题集
前端·算法
繁星蓝雨10 小时前
C++的设计与演进大纲(如何从C语言一步步成长为C++)
c语言·开发语言·c++·c++历史·c++演进
程序员阿鹏10 小时前
为什么MySQL InnoDB选择B+树?
数据结构·数据库·b树·sql·mysql·算法·缓存
yutian060610 小时前
C: Unix UTC 时间戳转为 UTC+8 北京时间
c语言·unix
AI 思录10 小时前
Prompt 事故档案(八):日常表达被标为“待校准”,AI 的爹味语法从哪里来
大数据·人工智能·算法·prompt·用户体验·ai合规
月光船幽幽10 小时前
跨范式映射的稳定接口设计
人工智能·python·算法