首页 > 其他分享 >leetcode-1790-easy

leetcode-1790-easy

时间:2022-10-30 13:35:40浏览次数:50  
标签:false s2 s1 1790 swap easy idx2 idx1 leetcode

Check if One String Swap Can Make Strings Equal

You are given two strings s1 and s2 of equal length. A string swap is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices.

Return true if it is possible to make both strings equal by performing at most one string swap on exactly one of the strings. Otherwise, return false.

 

Example 1:

Input: s1 = "bank", s2 = "kanb"
Output: true
Explanation: For example, swap the first character with the last character of s2 to make "bank".
Example 2:

Input: s1 = "attack", s2 = "defend"
Output: false
Explanation: It is impossible to make them equal with one string swap.
Example 3:

Input: s1 = "kelb", s2 = "kelb"
Output: true
Explanation: The two strings are already equal, so no string swap operation is required.
 

Constraints:

1 <= s1.length, s2.length <= 100
s1.length == s2.length
s1 and s2 consist of only lowercase English letters.

思路一: 遍历字符串,记录字符不同的位置,完全分类,有以下几种分类

  • 没有不同的位置 true
  • 有一个不同的位置 false
  • 有两个不同的位置 ?
  • 有三个以上不同的位置 false

只需要对第三种情况进行字符判断即可,其他三种情况可以直接给出 bool 值

public boolean areAlmostEqual(String s1, String s2) {
	int idx1 = -1;
	int idx2 = -1;
	for (int i = 0; i < s1.length(); i++) {
		if (s1.charAt(i) != s2.charAt(i)) {
			if (idx1 == -1) idx1 = i;
			else if (idx2 == -1) idx2 = i;
			else return false;
		}
	}

	if (idx1 == -1 && idx2 == -1) return true;
	if (idx1 != -1 && idx2 != -1) return (s1.charAt(idx1) == s2.charAt(idx2) && s1.charAt(idx2) == s2.charAt(idx1));
	return  false;
}

标签:false,s2,s1,1790,swap,easy,idx2,idx1,leetcode
From: https://www.cnblogs.com/iyiluo/p/16841083.html

相关文章

  • leetcode-2160-easy
    MinimumSumofFourDigitNumberAfterSplittingDigitsYouaregivenapositiveintegernumconsistingofexactlyfourdigits.Splitnumintotwonewintegers......
  • leetcode-1748-easy
    SumofUniqueElementsYouaregivenanintegerarraynums.Theuniqueelementsofanarrayaretheelementsthatappearexactlyonceinthearray.Returnthe......
  • leetcode102-二叉树的层序遍历
    102.二叉树的层序遍历有两种实现方法。第一种是递归,第二种是队列实现。第一种是看了别人的代码写出来的,第二种是自己写的。这道题的不能直接把遍历得到的数字直接塞进res......
  • EasyExcel导出Date类型格式处理
    EasyExcel导出Date类型格式处理​ 如果在导出的excel中有date时间类型的字段,直接导出会报错org.apache.poi.ss.usermodel.Cell.setCellValue(Ljava/time/LocalDateTime;)......
  • 刷题 LeetCode二叉树2
    代码随想录LeetCode102.二叉树的层序遍历carl二叉树遍历#层序遍历#队列#广度优先思路队首是待访问节点,每访问一个节点,将其左子和右子加入队列细节如何知道某......
  • LeetCode 题解 | 1. 两数之和 Javascript 版
    题目给定一个整数数组nums 和一个整数目标值target,请你在该数组中找出和为目标值target 的那 两个 整数,并返回它们的数组下标。你可以假设每种输入只会对应一个......
  • LeetCode 题解 | 3. 无重复字符的最长子串 Javascript
    /***@param{string}str*@returnsnumber*思路:1.start与range组合成一个窗口,窗口内的子串就是当前最长不重复的字符串*2.range每次循环递增*......
  • LeetCode 题解|6. Z 字形变换
    /***@param{string}s*@param{number}numRows*@return{string}*/varconvert=function(s,numRows){//存储结果constrows=[];//指针下一......
  • LeetCode 题解|9. 回文数
    /***@param{number}x*@return{boolean}*/varisPalindrome=function(x){if(x<0){returnfalse;}letnum=x;letreverse=0;wh......
  • LeetCode 题解|7. 整数反转
    /***@param{number}x*@return{number}*/varreverse=function(x){letres=0;while(x!=0){res=res*10+(x%10);//划重点......