1657. 确定两个字符串是否接近

1657. 确定两个字符串是否接近


题目链接:1657. 确定两个字符串是否接近

代码如下:

class Solution 
{
public:
    bool closeStrings(string word1, string word2) 
    {
        if(word1.size()!=word2.size())  {return false;}

        vector<int> word1_cnt(26,0);
        for(char c:word1)   {word1_cnt[c-'a']++;}

        vector<int> word2_cnt(26,0);
        for(char c:word2)   {word2_cnt[c-'a']++;}

        for(int i=0;i<26;i++)
        {
            if((word1_cnt[i]==0)!=(word2_cnt[i]==0))    {return false;}
        }

        sort(word1_cnt.begin(),word1_cnt.end());
        sort(word2_cnt.begin(),word2_cnt.end());
        return word1_cnt==word2_cnt;
    }
};

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 08:04:01       7 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 08:04:01       7 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 08:04:01       6 阅读
  4. Python语言-面向对象

    2024-07-11 08:04:01       9 阅读

热门阅读

  1. C# 预处理器指令

    2024-07-11 08:04:01       8 阅读
  2. CentOS命令格式及常用命令

    2024-07-11 08:04:01       8 阅读
  3. 深入理解基本数据结构:栈详解

    2024-07-11 08:04:01       6 阅读