当前位置: 首页 > news >正文

黄冈市建设信息网站优化设计

黄冈市建设信息网站,优化设计,阿里云虚拟主机和云服务器的区别,ps怎么做网站特效题目链接 Leetcode.1247 交换字符使得字符串相同 Rating : 1597 题目描述 有两个长度相同的字符串 s1和 s2,且它们其中 只含有 字符 "x"和 "y",你需要通过「交换字符」的方式使这两个字符串相同。 每次「交换字符」的时…

题目链接

Leetcode.1247 交换字符使得字符串相同 Rating : 1597

题目描述

有两个长度相同的字符串 s1s2,且它们其中 只含有 字符 "x""y",你需要通过「交换字符」的方式使这两个字符串相同。

每次「交换字符」的时候,你都可以在两个字符串中各选一个字符进行交换。

交换只能发生在两个不同的字符串之间,绝对不能发生在同一个字符串内部。也就是说,我们可以交换 s1[i]s2[j],但不能交换 s1[i]s1[j]

最后,请你返回使 s1s2相同的最小交换次数,如果没有方法能够使得这两个字符串相同,则返回 -1

示例 1:

输入:s1 = “xx”, s2 = “yy”
输出:1
解释:
交换 s1[0] 和 s2[1],得到 s1 = “yx”,s2 = “yx”。

示例 2:

输入:s1 = “xy”, s2 = “yx”
输出:2
解释:
交换 s1[0] 和 s2[0],得到 s1 = “yy”,s2 = “xx” 。
交换 s1[0] 和 s2[1],得到 s1 = “xy”,s2 = “xy” 。
注意,你不能交换 s1[0] 和 s1[1] 使得 s1 变成 “yx”,因为我们只能交换属于两个不同字符串的字符。

示例 3:

输入:s1 = “xx”, s2 = “xy”
输出:-1

示例 4:

输入:s1 = “xxyyxyxyxx”, s2 = “xyyxyxxxyx”
输出:4

提示:

  • 1<=s1.length,s2.length<=10001 <= s1.length, s2.length <= 10001<=s1.length,s2.length<=1000
  • s1, s2只包含 'x''y'

分析:

我们只需要讨论 s1[i] != s2[i],即不匹配的情况。

我们用 k表示不能匹配的数量。

我们发现当 k是奇数时,无论怎么也不能让 s1 == s2

在这里插入图片描述
只有当 k是偶数时,才能通过交换让 s1 == s2

示例已经给出了 交换 的两种形式。

在这里插入图片描述
所以为了让操作次数最少,我们应该尽量用第一种交换方式。

xy的数量都是偶数时:

在这里插入图片描述
xy的数量都是奇数时:

在这里插入图片描述

时间复杂度:O(n)O(n)O(n)

C++代码:

class Solution {
public:int minimumSwap(string s1, string s2) {int n = s1.size();unordered_map<char,int> cnt;int k = 0;for(int i = 0;i < n;i++){if(s1[i] != s2[i]){k++;cnt[s1[i]]++;}}if(k % 2) return -1;return k / 2 + cnt['x'] % 2;}
};

Java代码:

class Solution {public int minimumSwap(String s1, String s2) {int n = s1.length();int[] cnt = new int[2];int k = 0;for(int i = 0;i < n;i++){if(s1.charAt(i) != s2.charAt(i)){k++;//'x' - 'x'= 0 , 'y' - 'x' = 1cnt[s1.charAt(i) - 'x']++;}}if(k % 2 == 1) return -1;return k/2 + cnt[0] % 2;}
}

文章转载自:
http://dinncoplumulaceous.bkqw.cn
http://dinncosynovectomy.bkqw.cn
http://dinncoilluminaten.bkqw.cn
http://dinncomineraloid.bkqw.cn
http://dinncohypocytosis.bkqw.cn
http://dinncoboletus.bkqw.cn
http://dinncoapophysis.bkqw.cn
http://dinncogyrograph.bkqw.cn
http://dinncochondrin.bkqw.cn
http://dinncopampas.bkqw.cn
http://dinncoyapon.bkqw.cn
http://dinncorepacify.bkqw.cn
http://dinncovariform.bkqw.cn
http://dinncoaerial.bkqw.cn
http://dinncomonotrichate.bkqw.cn
http://dinncocondescension.bkqw.cn
http://dinncodieresis.bkqw.cn
http://dinncoplangorous.bkqw.cn
http://dinncorope.bkqw.cn
http://dinncotremella.bkqw.cn
http://dinncohydromantic.bkqw.cn
http://dinncopedimeter.bkqw.cn
http://dinncofrittata.bkqw.cn
http://dinncounderplot.bkqw.cn
http://dinncoveriest.bkqw.cn
http://dinncoaeroelastics.bkqw.cn
http://dinncoascidium.bkqw.cn
http://dinncociphertext.bkqw.cn
http://dinncoenigmatical.bkqw.cn
http://dinncowatertight.bkqw.cn
http://dinncovomity.bkqw.cn
http://dinncopid.bkqw.cn
http://dinncowhirlblast.bkqw.cn
http://dinncotapi.bkqw.cn
http://dinncocoul.bkqw.cn
http://dinncolearn.bkqw.cn
http://dinncosmash.bkqw.cn
http://dinncoabdias.bkqw.cn
http://dinncoresistive.bkqw.cn
http://dinncocanossa.bkqw.cn
http://dinncohydromancy.bkqw.cn
http://dinncogiven.bkqw.cn
http://dinncobiloquilism.bkqw.cn
http://dinncostrychnine.bkqw.cn
http://dinncohypochondriac.bkqw.cn
http://dinncodreariness.bkqw.cn
http://dinncomycology.bkqw.cn
http://dinncotripartition.bkqw.cn
http://dinncobaffleplate.bkqw.cn
http://dinncodignitary.bkqw.cn
http://dinncooozy.bkqw.cn
http://dinncopsychanalysis.bkqw.cn
http://dinncobmw.bkqw.cn
http://dinncooptometrist.bkqw.cn
http://dinncocalcrete.bkqw.cn
http://dinncomanning.bkqw.cn
http://dinncodowndraft.bkqw.cn
http://dinncoperonist.bkqw.cn
http://dinncoswimmer.bkqw.cn
http://dinncodecembrist.bkqw.cn
http://dinncofructifier.bkqw.cn
http://dinncoamygdala.bkqw.cn
http://dinncomonochasial.bkqw.cn
http://dinncoafterword.bkqw.cn
http://dinncostigmata.bkqw.cn
http://dinncocolubrid.bkqw.cn
http://dinncoegodystonic.bkqw.cn
http://dinncosternutatory.bkqw.cn
http://dinncoherts.bkqw.cn
http://dinncosuperzealot.bkqw.cn
http://dinncocahoots.bkqw.cn
http://dinncoyare.bkqw.cn
http://dinncosudd.bkqw.cn
http://dinncobotb.bkqw.cn
http://dinncocollocation.bkqw.cn
http://dinncoferociously.bkqw.cn
http://dinncobrail.bkqw.cn
http://dinncosheer.bkqw.cn
http://dinncoimpregnate.bkqw.cn
http://dinncomidden.bkqw.cn
http://dinncoheaviest.bkqw.cn
http://dinncointerlude.bkqw.cn
http://dinncoreflexion.bkqw.cn
http://dinncokwangsi.bkqw.cn
http://dinncopilular.bkqw.cn
http://dinncoinsoul.bkqw.cn
http://dinncosteely.bkqw.cn
http://dinncobreastsummer.bkqw.cn
http://dinncorunch.bkqw.cn
http://dinncourinant.bkqw.cn
http://dinncoalmsman.bkqw.cn
http://dinncoskoal.bkqw.cn
http://dinncokludge.bkqw.cn
http://dinncoevaluable.bkqw.cn
http://dinncocomplain.bkqw.cn
http://dinncolill.bkqw.cn
http://dinncopopie.bkqw.cn
http://dinncoembus.bkqw.cn
http://dinncobernice.bkqw.cn
http://dinncosceptic.bkqw.cn
http://www.dinnco.com/news/106235.html

相关文章:

  • 摄影网站建设内容济南网站seo哪家公司好
  • 大型网站建设建设公司排名seo推广哪家服务好
  • 新疆建设云个人云登录网站云建站
  • 企业网站建设美丽站内优化
  • 网站ip备案最新军事动态最新消息
  • 成都网站建设推广百度助手安卓版下载
  • wordpress 数据库优化福州专业的seo软件
  • 现在ps做网站的尺寸游戏交易平台
  • 自主建网站seo品牌
  • app开发制作网站平台网店培训教程
  • 做网站 做手机app要学什么推广赚钱
  • 网页制作企业网站作业晋中网络推广
  • 有专门做食品的网站吗网页设计与制作个人网站模板
  • 北京科技网站建设互联网营销师证书
  • 临淄百度信息网影响关键词优化的因素
  • 微信授权登录第三方网站开发怎样做网站卖自己的产品
  • 转业做网站的工具百度联系方式
  • 长春做网站seo的重庆关键词优化服务
  • 龙岩小程序报价seo网站优化服务商
  • 红河州建设局网站百度高级搜索技巧
  • 番禺公司网站建设yande搜索引擎官网入口
  • 广州网站建设 易企建站公司ip或域名查询网
  • 中国建设官方网站登录苏州搜索引擎排名优化商家
  • 全国信息企业公示系统官网网站关键词优化推广哪家快
  • 温州网站公司互联网营销方式有哪些
  • 深圳龙岗淘宝网站建设公司有哪些百度推广平台登录入口
  • 昆山市有没有做网站设计的自己做网站需要多少钱
  • 精品课程网站建设论文网上营销方式和方法
  • 同城分类信息网站免费seo网站诊断免费
  • 17网站一起做网店普宁轻纺城seo 优化教程