本文主要是介绍Leetcode—859.亲密字符串【简单】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
2023每日刷题(六十三)
Leetcode—859.亲密字符串
💩山实现代码
class Solution {
public:bool buddyStrings(string s, string goal) {int len1 = s.size(), len2 = goal.size();int cnt = 0;int flag = 0;int flag2 = 0;int odd = -1;int a[26] = {0};if(len1 != len2) {return false;}for(int i = 0; i < len1; i++) {if(s[i] != goal[i]) {if(odd != -1 && s[odd] != goal[i] && s[i] != goal[odd]) {return false;} if(odd == -1) {odd = i;}if(s[odd] == goal[i] && s[i] == goal[odd]) {odd = -1;}cnt++;}a[s[i] - 'a'] += 1;if(a[s[i] - 'a'] >= 2) {flag2 = 1;}if(cnt > 2) {return false;}if(i < len1 && s[i] == s[i + 1]) {flag = 1;}}if(cnt == 2 && odd != -1) {return false;} if(cnt == 2) {return true;}if((flag || flag2)&& cnt == 0) {return true;}return false;}
};
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!
这篇关于Leetcode—859.亲密字符串【简单】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!