Buddy Strings Solutions in C++
Number 859
Difficulty Easy
Acceptance 27.4%
Link LeetCode
Other languages —
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/buddy-strings/description/// Author : Hao Chen// Date : 2018-06-27class Solution {public:bool buddyStrings(string A, string B) {if (A.size() != B.size()) return false;if (A.size()<2) return false;bool bRepeat = false;bool map[26] = {false};int idx[2], diffCnt=0;for (int i=0; i<A.size(); i++){if (map[A[i]-'a']) { bRepeat = true;}map[A[i]-'a']=true;if ( A[i] != B[i] ) {if (diffCnt>=2) return false;idx[diffCnt++] = i;}}//if A == B and there has repeated chars , then return trueif (diffCnt==0 && bRepeat) return true;return (A[idx[0]] == B[idx[1]] && A[idx[1]] == B[idx[0]]);}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/buddy-strings/description//// Author : liuyubobobo/// Time : 2018-06-23#include <iostream>#include <unordered_map>using namespace std;/// Scan and Compare/// Time Complexity: O(nlogn)/// Space Complexity: O(n)class Solution {public:bool buddyStrings(string A, string B) {if(A.size() != B.size())return false;unordered_map<char, char> diff;int diff_num = 0;for(int i = 0 ; i < A.size() ; i ++)if(A[i] != B[i]){diff[A[i]] = B[i];diff_num ++;}if(diff_num == 0)return atLeastTwoSame(A);if(diff_num != 2)return false;unordered_map<char, char>::iterator iter1 = diff.begin();unordered_map<char, char>::iterator iter2 = diff.begin();iter2 ++;return iter1->first == iter2->second && iter1->second == iter2->first;}private:bool atLeastTwoSame(const string& s){int freq[26];memset(freq, 0, sizeof(freq));for(char c: s){freq[c - 'a'] ++;if(freq[c - 'a'] >= 2)return true;}return false;}};void print_bool(bool res){cout << (res ? "True" : "False") << endl;}int main() {print_bool(Solution().buddyStrings("ab", "ba")); // trueprint_bool(Solution().buddyStrings("ab", "ab")); // falseprint_bool(Solution().buddyStrings("ab", "ba")); // trueprint_bool(Solution().buddyStrings("aaaaaaabc", "aaaaaaacb")); // trueprint_bool(Solution().buddyStrings("", "aa")); // falsereturn 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/buddy-strings/description//// Author : liuyubobobo/// Time : 2018-06-25#include <iostream>using namespace std;/// Scan and Compare/// Time Complexity: O(n)/// Space Complexity: O(1)class Solution {public:bool buddyStrings(string A, string B) {if(A.size() != B.size())return false;if(A == B)return atLeastTwoSame(A);int first = -1, second = -1;for(int i = 0 ; i < A.size(); i++)if(A[i] != B[i]){if(first == -1)first = i;else if(second == -1)second = i;elsereturn false;}return A[first] == B[second] && A[second] == B[first];}private:bool atLeastTwoSame(const string& s){int freq[26];memset(freq, 0, sizeof(freq));for(char c: s){freq[c - 'a'] ++;if(freq[c - 'a'] >= 2)return true;}return false;}};void print_bool(bool res){cout << (res ? "True" : "False") << endl;}int main() {print_bool(Solution().buddyStrings("ab", "ba")); // trueprint_bool(Solution().buddyStrings("ab", "ab")); // falseprint_bool(Solution().buddyStrings("ab", "ba")); // trueprint_bool(Solution().buddyStrings("aaaaaaabc", "aaaaaaacb")); // trueprint_bool(Solution().buddyStrings("", "aa")); // falsereturn 0;}