首页 > 其他分享 >LeetCode 859. Buddy Strings

LeetCode 859. Buddy Strings

时间:2022-08-28 15:35:47浏览次数:80  
标签:return goal charAt get Strings swap diff LeetCode Buddy

原题链接在这里:https://leetcode.com/problems/buddy-strings/

题目:

Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false.

Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j].

  • For example, swapping at indices 0 and 2 in "abcd" results in "cbad".

Example 1:

Input: s = "ab", goal = "ba"
Output: true
Explanation: You can swap s[0] = 'a' and s[1] = 'b' to get "ba", which is equal to goal.

Example 2:

Input: s = "ab", goal = "ab"
Output: false
Explanation: The only letters you can swap are s[0] = 'a' and s[1] = 'b', which results in "ba" != goal.

Example 3:

Input: s = "aa", goal = "aa"
Output: true
Explanation: You can swap s[0] = 'a' and s[1] = 'a' to get "aa", which is equal to goal.

Constraints:

  • 1 <= s.length, goal.length <= 2 * 104
  • s and goal consist of lowercase letters.

题解:

If length is different, then it can't bu buddy strings, return false.

If s and goal are equal, then at least there should be duplciate chars to be swapped.

Otherwise, there could be only two difference and corresponding place could be swapped.

Time Complexity: O(m). m = s.length().

Space: O(m).

AC Java:

 1 class Solution {
 2     public boolean buddyStrings(String s, String goal) {
 3         int m = s.length();
 4         int n = goal.length();
 5         if(m != n){
 6             return false;
 7         }
 8         
 9         if(s.equals(goal)){
10             HashSet<Character> hs = new HashSet<>();
11             for(char c : s.toCharArray()){
12                 hs.add(c);
13             }
14             
15             return hs.size() != m;
16         }
17         
18         List<Integer> diff = new ArrayList<>();
19         for(int i = 0; i < m; i++){
20             if(s.charAt(i) != goal.charAt(i)){
21                 diff.add(i);
22             }
23         }
24         
25         return diff.size() == 2 && s.charAt(diff.get(0)) == goal.charAt(diff.get(1)) && s.charAt(diff.get(1)) == goal.charAt(diff.get(0));
26     }
27 }

 

标签:return,goal,charAt,get,Strings,swap,diff,LeetCode,Buddy
From: https://www.cnblogs.com/Dylan-Java-NYC/p/16632831.html

相关文章

  • [Google] LeetCode 359 Logger Rate Limiter
    Designaloggersystemthatreceivesastreamofmessagesalongwiththeirtimestamps.Eachuniquemessageshouldonlybeprintedatmostevery10seconds(i.e......
  • leetcode-793. 阶乘函数后 K 个零
    793.阶乘函数后K个零图床:blogimg/刷题记录/leetcode/793/刷题代码汇总:https://www.cnblogs.com/geaming/p/16428234.html题目思路首先我们令\(zeta(x)\)为\(x!\)......
  • leetcode-172. 阶乘后的零
    172.阶乘后的零图床:blogimg/刷题记录/leetcode/172/刷题代码汇总:https://www.cnblogs.com/geaming/p/16428234.html题目思路n!中有几个0与[1,n]中出现多少个5的因数......
  • LeetCode 1166. Design File System
    原题链接在这里:https://leetcode.com/problems/design-file-system/题目:Youareaskedtodesignafilesystem thatallowsyoutocreatenewpathsandassociatet......
  • LeetCode/阶乘后的零
    1.返回尾零数量可以转换为求质因子为2和5数量的较小值,实际上就是求质因子为5的数量classSolution{public:inttrailingZeroes(intn){intans=0;......
  • 【重要】LeetCode 662. 二叉树最大宽度
    题目链接注意事项根据满二叉树的节点编号规则:若根节点编号为u,则其左子节点编号为u<<1,其右节点编号为u<<1|1。一个朴素的想法是:我们在DFS过程中使用两个哈希表......
  • leetcode 647. Palindromic Substrings回文子串(中等)
    一、题目大意给你一个字符串s,请你统计并返回这个字符串中回文子串的数目。回文字符串是正着读和倒过来读一样的字符串。子字符串是字符串中的由连续字符组成的一......
  • leetcode139:单词拆分
    packagecom.mxnet;importjava.util.HashSet;importjava.util.List;publicclassSolution139{publicstaticvoidmain(String[]args){}/**......
  • LeetCode刷题23-在排序数组中查找元素的第一个和最后一个位置
    importjava.util.Arrays;/***功能描述**@authorASUS*@version1.0*@Date2022/8/27*/publicclassMain06{publicstaticvoidmain(String[]......
  • leetcode169:多数元素
    packagecom.mxnet;importjava.util.HashMap;importjava.util.Set;publicclassSolution169{publicstaticvoidmain(String[]args){}/**......