859. Buddy Strings
class Solution
{
public:
bool buddyStrings(string A, string B)
{
int lenA=A.length();
int lenB=B.length();
if(lenA!=lenB)
return false;
unordered_set<char>cset(A.begin(),A.end());
if(A==B&&cset.size()<lenA)
return true;
vector<int> diff;
for(int i=;i<lenA;i++)
{
if(A[i]!=B[i])
diff.push_back(i);
}
return diff.size()==&&A[diff[]]==B[diff[]]&&A[diff[]]==B[diff[]];
}
};
分两种情况,一种是A==B,另一种是A!=B
相等前提下,只要A中有两个相同字母,就满足条件
不等前提下,有且只有两个交叉字母才满足条件
859. Buddy Strings的更多相关文章
- 【Leetcode_easy】859. Buddy Strings
problem 859. Buddy Strings solution: class Solution { public: bool buddyStrings(string A, string B) ...
- 859. Buddy Strings - LeetCode
Question 859. Buddy Strings Solution 题目大意: 两个字符串,其中一个字符串任意两个字符互换后与另一个字符串相等,只能互换一次 思路: diff 记录不同字符数 两 ...
- 859. Buddy Strings (wrong 4 times so many cases to test and consider) if else**
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
- leetcode 859. Buddy Strings
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
- 【LeetCode】859. Buddy Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- LeetCode 859. 亲密字符串(Buddy Strings) 23
859. 亲密字符串 859. Buddy Strings 题目描述 给定两个由小写字母构成的字符串 A 和 B,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true:否则返 ...
- [LeetCode] Buddy Strings 伙计字符串
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
- [Swift]LeetCode859. 亲密字符串 | Buddy Strings
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
- [LeetCode] 859. Buddy Strings_Easy
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
随机推荐
- set<pair<int,int> >的用法
例题链接:https://vjudge.net/contest/236677#problem/D 题意:首先输入两个数字n,m.n表示有n张桌子,编号从1到n,m表示有m个操作, 然后接下来一行有n个 ...
- 非常棒的轨迹插件Better Trails v1.4.6
点击下载
- Springboot学习03-SpringMVC自动配置
Springboot学习03-SpringMVC自动配置 前言 在SpringBoot官网对于SpringMVCde 自动配置介绍 1-原文介绍如下: Spring MVC Auto-configur ...
- json的内容回顾
复习一下json这个模块 import json s = '{"name":"cui","age":12}' # 这里外部必须是单引号,内部 ...
- C盘文件过大,C盘空间莫名丢失,pagefile.sys文件
设置显示隐藏文件和受保护的系统文件后此文件会显示pagefile.sys这个是window的页面文件,作为系统的虚拟内存使用,和你的物理内存一样大,你内存已经很大了,不用开虚拟内存了.在系统高级属性下 ...
- java图片操作--生成与原图对称的图片
java图片操作--生成与原图对称的图片 package com.pay.common.util; import java.awt.image.BufferedImage; import java.i ...
- jQuery和js使用点滴
1.checkbox全选按钮 <input type="checkbox" name="allcheck" id="allcheck" ...
- 32-改变eclipse默认的Tomcat部署路径
转载:https://www.cnblogs.com/helf/p/9433588.html eclipse中默认的项目部署路径是在项目的路径,不像myeclipse那样部署后项目在Tomcat的安装 ...
- vue项目优化之路由懒加载
const login = () =>import('@/views/login'); export default new Router({ routes:[ { path:'/login', ...
- 使用clear来清除localStorage保存对象的全部数据
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...