【LeetCode】winter vacation training

本文介绍了C++中的字符串操作函数substr和解决LeetCode题目中的有效字母异位词问题,以及如何使用unordered_map检查字符串解码的字符频数差异。
摘要由CSDN通过智能技术生成

在这里插入图片描述

欢迎来到Cefler的博客😁
🕌博客主页:那个传说中的man的主页
🏠个人专栏:题目解析
🌎推荐文章:【LeetCode】winter vacation training

在这里插入图片描述


👉🏻 有效的字母异位词

原题链接:有效的字母异位词

mycode:

class Solution {
public:
  
    bool isAnagram(string s, string t) {
        map<char,int> ms;
        for(auto e:s)
        {
            ms[e]++;
        }
          for(auto e:t)
        {
            ms[e]--;
        }
        for(auto e:ms)
        {
            if(e.second!=0)
                return false;
        }
       
        return true;
    }
};

👉🏻 判断字符串的两半是否相似

原题链接:判断字符串的两半是否相似

mycode:

class Solution {
public:
    bool halvesAreAlike(string s) {
        string str = "aeiouAEIOU";
        int half = s.size()/2;
        string left = s.substr(0,half),right = s.substr(half);
        int count1 = 0,count2 = 0;
        for(int i = 0;i<half;i++)
        {
            if((string(1,left[i])).find_first_of(str)!=string::npos)
                count1++;
            if((string(1,right[i])).find_first_of(str)!=string::npos)
                count2++;
        }
        if(count1==count2)return true;
        else return false;
    }
};

substr是C++中的一个字符串操作函数,用于从给定字符串中提取子字符串。

substr函数的语法如下:

string substr(size_t pos = 0, size_t count = npos) const;

参数说明:

  • pos:要提取子字符串的起始位置。默认为0,表示从字符串的开头开始。
  • count:要提取的字符数。默认为npos,表示提取从起始位置到字符串末尾的所有字符。

substr函数返回一个新的string对象,其中包含了从原始字符串中提取的子字符串。

以下是一个使用substr函数的示例:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str = "Hello, World!";
    
    string sub1 = str.substr(7); // 从位置7开始提取子字符串
    cout << "sub1: " << sub1 << endl; // 输出: World!
    
    string sub2 = str.substr(0, 5); // 从位置0开始提取5个字符的子字符串
    cout << "sub2: " << sub2 << endl; // 输出: Hello
    
    return 0;
}

输出结果:

sub1: World!
sub2: Hello

在上面的示例中,我们定义了一个字符串str,然后使用substr函数从该字符串中提取了两个子字符串。第一个子字符串sub1从位置7开始提取,即字符串中的"World!"部分。第二个子字符串sub2从位置0开始提取,提取了前5个字符,即字符串中的"Hello"部分。

需要注意的是,substr函数返回的是一个新的string对象,原始字符串本身并没有改变。

👉🏻 有效的字母异位词

原题链接:有效的字母异位词

mycode:

class Solution {
public:
    bool checkAlmostEquivalent(string word1, string word2) {
        map<char,int> w1,w2;
        for(auto e:word1) w1[e]++;
        for(auto e:word2) w2[e]++;

        for(auto x:w1)
        if(x.second-w2[x.first]>3)return false;
        for(auto x:w2)
        if(x.second-w1[x.first]>3)return false;

        return true;
    }
};

官方题解:

class Solution {
public:
    bool checkAlmostEquivalent(string word1, string word2) {
         unordered_map<char, int> freq;   // 频数差哈希表
        for (auto ch: word1){
            ++freq[ch];
        }
        for (auto ch: word2){
            --freq[ch];
        }
        // 判断每个字符频数差是否均小于等于 3
        return all_of(freq.begin(), freq.end(), [](auto&& x) { return abs(x.second) <= 3; });

    }
};

👉🏻 字符串解码

原题链接:字符串解码

mycode:

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值