烟台网站建设哪家好呢,新会网站建设公司,谷歌做网站推广,登封网站制作网站建设题目来源#xff1a; leetcode题目#xff0c;网址#xff1a;1657. 确定两个字符串是否接近 - 力扣#xff08;LeetCode#xff09;
解题思路#xff1a; 当一个字符串中出现的字符全部在另一个字符串中出现并且 两字符串各字符出现次数排序后的 有序序列相同 时 leetcode题目网址1657. 确定两个字符串是否接近 - 力扣LeetCode
解题思路 当一个字符串中出现的字符全部在另一个字符串中出现并且 两字符串各字符出现次数排序后的 有序序列相同 时两字符串接近。
解题代码
class Solution {
public:bool closeStrings(string word1, string word2) {if(word1.length()!word2.length()){return false;}vectorint cnt1(26,0);vectorint cnt2(26,0);for(int i0;iword1.length();i){cnt1[word1[i]-a];cnt2[word2[i]-a];}for(int i0;icnt1.size();i){if((cnt1[i]0 cnt2[i]0) || (cnt2[i]0 cnt1[i]0)){return false;}}sort(cnt1.begin(),cnt1.end());sort(cnt2.begin(),cnt2.end());for(int i0;icnt1.size();i){if(cnt1[i]!cnt2[i]){return false;}}return true;}
};
总结 官方题解也是计数。