微信开发中 ACCESS TOKEN 过期失效的解决方案详解
550
2022-10-20
[leetcode] 859. Buddy Strings
Description
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters in A so that the result equals B. Example 1:
Input: A = "ab", B = "ba"Output: true
Example 2:
Input: A = "ab", B = "ab"Output: false
Example 3:
Input: A = "aa", B = "aa"Output: true
Example 4:
Input: A = "aaaaaaabc", B = "aaaaaaacb"Output: true
Example 5:
Input: A = "", B = "aa"Output: false
Note:
0 <= A.length <= 200000 <= B.length <= 20000A and B consist only of lowercase letters.
分析
题目的意思是:有两个字符串,其中一个字符串只交换字符一次,看两个字符串是否相等
这是一道easy类型的题目,开始大家可能都觉得这是一道dp的问题,其实简单解法既可以,废话不多说,直接看代码。如果字符串A与字符串B不相等,则直接返回false;然后遍历字符串A,B,记录字符串中字符不相等的位置,然后把上面例子的五种情况都考虑到就行了。
代码
class Solution {public: bool buddyStrings(string A, string B) { int m=A.length(); int n=B.length(); if(m!=n){ return false; } int pos=-1; bool isSwap=false; bool isRepeat=false; vector
参考文献
859. Buddy Strings
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~