Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【每日一题】- 2019-11-11 859.亲密字符串 #229

Open
azl397985856 opened this issue Nov 11, 2019 · 5 comments
Open

【每日一题】- 2019-11-11 859.亲密字符串 #229

azl397985856 opened this issue Nov 11, 2019 · 5 comments

Comments

@azl397985856
Copy link
Owner

@azl397985856 azl397985856 commented Nov 11, 2019

给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true ;否则返回 false 。

 

示例 1:

输入: A = "ab", B = "ba"
输出: true
示例 2:

输入: A = "ab", B = "ab"
输出: false
示例 3:

输入: A = "aa", B = "aa"
输出: true
示例 4:

输入: A = "aaaaaaabc", B = "aaaaaaacb"
输出: true
示例 5:

输入: A = "", B = "aa"
输出: false
 

提示:

0 <= A.length <= 20000
0 <= B.length <= 20000
A 和 B 仅由小写字母构成。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/buddy-strings
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

@wangyuxiang0829

This comment has been minimized.

Copy link

@wangyuxiang0829 wangyuxiang0829 commented Nov 11, 2019

class Solution {
public:
    bool buddyStrings(string A, string B) {
        int m = A.size(), n = B.size(), c = 0;
        if (m <= 1 || m != n) return false;
        vector<int> v;
        int map[26] = {0};
        bool flag = false;
        for (int i = 0; i < n; i++) {
            if (int m = ++map[A[i] - 'a']; m >= 2)
                flag = true;
            if (A[i] != B[i]) {
                c++;
                if (c > 2) return false;
                v.push_back(i);
            }
        }
        if (c == 1) return false;
        else if (c == 0) return flag;
        else return A[v[0]] == B[v[1]] && A[v[1]] == B[v[0]];
    }
};
@Stephen-Pierre

This comment has been minimized.

Copy link

@Stephen-Pierre Stephen-Pierre commented Dec 4, 2019

python3实现

class Solution:
    def buddyStrings(self, A: str, B: str) -> bool:
        s1 = ''
        s2 = ''
        if len(A) != len(B):
            return False
        elif A == B:
            if len(set(A)) < len(A):
                return True
            else:
                return False
        else:
            for i in range(len(A)):
                if A[i] != B[i]:
                    s1 += A[i]
                    s2 += B[i]
            if s1[0] == s2[1] and s1[1] == s2[0] and len(s1)==2:
                return True
            else:
                return False
@wptercel

This comment has been minimized.

Copy link

@wptercel wptercel commented Dec 13, 2019

class Solution {
    public boolean buddyStrings(String A, String B) {
        int lenA = A.length();
        int lenB = B.length();
        
        if(lenA < 2 || lenA != lenB) {
            return false;
        }
        
        int diffCount = 0;
        char char1 = 0;
        char char2 = 0;
        HashSet<Character> charSet = new HashSet<>();;
        for(int i = 0; i < lenA; i++) {
            charSet.add(A.charAt(i));
            if(A.charAt(i) != B.charAt(i)) {
                if(diffCount == 0) {
                    char1 = A.charAt(i);
                    char2 = B.charAt(i);
                }
                else if(diffCount == 1) {
                    if(char1 != B.charAt(i) || char2 != A.charAt(i)) {
                        return false;
                    }
                }
                diffCount++;
            }
        }
        return diffCount == 2 || charSet.size() < lenA;
    }
}
@XRuHa

This comment has been minimized.

Copy link

@XRuHa XRuHa commented Dec 21, 2019

C 实现:

void Swap(char* pCh1, char* pCh2)
{
char temp = 0;
temp = *pCh1;
*pCh1 = *pCh2;
*pCh2 = temp;
}

BOOL buddyStrings(CHAR pStrA[], CHAR pStrB[])
{
size_t length = strlen(pStrA);
BOOL blFlag = FALSE;
for (size_t i = 1; i < length; i++)
{
Swap(&pStrA[i-1], &pStrA[i]);
if (0 == strcmp(pStrA, pStrB))
{
blFlag = TRUE;
break;
}
}
return blFlag ? TRUE : FALSE;
}

@Casper-Mars

This comment has been minimized.

Copy link

@Casper-Mars Casper-Mars commented Dec 27, 2019

bool isBuddyString(string str1, string str2) {
/判断是否为空和长度是否相等/
if (str1.empty() || str2.empty() || str1.size() != str2.size()) {
return false;
}
string::iterator str1b = str1.begin();
string::iterator str2b = str2.begin();
int swapTime = 0;
/遍历字符串/
while (str1b != str1.end()) {
/如果对应位置不相同/
if (*str1b != str2b) {
/两个字符串当前位置和下一位置的交叉比对,相同则记录交换次数,不同则直接返回false/
if (
(str1b + 1) == *str2b && *(str2b + 1) == *str1b) {
/交换次数大于1次返回false/
if (swapTime >= 1) {
return false;
} else {
swapTime++;
str1b++;
str2b++;
}
} else {
return false;
}
}
str1b++;
str2b++;
}
return true;
}

romanbrickie pushed a commit to romanbrickie/leetcode-1 that referenced this issue Jan 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
6 participants
You can’t perform that action at this time.