NO.1
 
 代码实现:
 #include <iostream>
 #include<string>
 using namespace std;
 
int n,k,t;
string s;
int func()
{
    int ret=0;
    for(int i=0;i<n;i++)
    {
        char ch=s[i];
        if(ch=='L') ret-=1;
        else
        {
            if(i-1>=0&&i-2>=0&&s[i-1]=='W'&&s[i-2]=='W') ret+=k;
            else
            {
                ret+=1;
            }
        }
}
    return ret;
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>k>>s;
        cout<<func()<<endl;
    }
    return 0;
}
NO.2
 
 思路:双指针+滑动窗口,哈希判断是否有重复字符,如果有就出窗口,出窗口就更新长度,right++继续遍历。
 代码实现:
class Solution {
public:
    int hash[100010]={0};
    int maxLength(vector<int>& arr) {
        int left=0,right=0;
        int n=arr.size();
        int ret=0;
        while(right<n)
        {
            hash[arr[right]]++;
            while(hash[arr[right]]>1)
            {
                hash[arr[left]]--;
                left++;
            }
            ret=max(ret,right-left+1);
            right++;
        }
        return ret;
    }
};
NO.3
 
 
 思路:用哈希表统计出现次数最多的字符,再判断是否可以重排,如果次数大于(n+1)/2就不能重排,再处理出现次数最多的字符,间隔一个就摆放,如果摆放的位置大于n,就从1开始摆放,也是间隔一个格子,最后输出就可以了。
代码实现:
#include<iostream>
using namespace std;
const int N=1e5+10;
char s[N];
char ret[N];
int n;
int main()
{
    cin>>n>>s;
    int hash[26]={0};
    char maxchar=0;
    int maxcount=0;
    
    for(int i=0;i<n;i++)
    {
        int index=s[i]-'a';
        if(++hash[index]>maxcount)
        {
          maxchar=s[i];
        maxcount=hash[index];
            
        }
    }
    
    if(maxcount>(n+1)/2) cout<<"no"<<endl;
    else{
        cout<<"yes"<<endl;
        int i=0;
        while(maxcount--)
        {
         ret[i]=maxchar;
            i+=2;
        }
        for(int j=0;j<26;j++)
        {
            if(hash[j]&&j+'a'!=maxchar)
            {
                while(hash[j]--)
                {
                    if(i>=n) i=1;
                    ret[i]=j+'a';
                    i+=2;
                }
            }
        }
        for(int j=0;j<n;j++) cout<<ret[j];
        cout<<endl;
    }
    return 0;
}


















![[笔记] Win11 Microsoft Store App 离线下载](https://img-blog.csdnimg.cn/direct/1d01177b5fbf4c349746d7a1b31b08ae.gif)
