ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 854. K-Similar Strings

2022-08-29 05:30:44  阅读:229  来源: 互联网

标签:854 cur int s2 s1 ind Similar LeetCode String


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

题目:

Strings s1 and s2 are k-similar (for some non-negative integer k) if we can swap the positions of two letters in s1 exactly k times so that the resulting string equals s2.

Given two anagrams s1 and s2, return the smallest k for which s1 and s2 are k-similar.

Example 1:

Input: s1 = "ab", s2 = "ba"
Output: 1

Example 2:

Input: s1 = "abc", s2 = "bca"
Output: 2

Constraints:

  • 1 <= s1.length <= 20
  • s2.length == s1.length
  • s1 and s2 contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}.
  • s2 is an anagram of s1.

题解:

The smallest swap, we could use BFS.

For a current string, if cur.equals(s2), return returns current level.

If not, we get all its neighbors. 

To get a neighbor, we first find the different char between cur and s2, mark its index as ind. 

Then find the next char where cur.charAt(j) == s2.charAt(ind).

Time Complexity: O(k*n^2). n = s1.length(). k = swap count, could be up to n. There could be k level BFS iteration. In each iteration, for each cur, we could find n neighbor, totally there could be n node in the que, thus there are n^2 neighbors.

Space: O(n^2).

AC Java:

 1 class Solution {
 2     public int kSimilarity(String s1, String s2) {
 3         LinkedList<String> que = new LinkedList<>();
 4         HashSet<String> visited = new HashSet<>();
 5         que.add(s1);
 6         visited.add(s1);
 7         int level = 0;
 8         
 9         while(!que.isEmpty()){
10             int size = que.size();
11             while(size-- > 0){
12                 String cur = que.poll();
13                 if(cur.equals(s2)){
14                     return level;
15                 }
16                 
17                 for(String can : getNei(cur, s2)){
18                     if(visited.contains(can)){
19                         continue;
20                     }
21                     
22                     que.add(can);
23                     visited.add(can);
24                 }
25             }
26             
27             level++;
28         }
29         
30         return -1;
31     }
32     
33     private List<String> getNei(String s1, String s2){
34         char [] s1Arr = s1.toCharArray();
35         char [] s2Arr = s2.toCharArray();
36         int n = s1.length();
37         int ind = 0;
38         while(ind < n && s1Arr[ind] == s2Arr[ind]){
39             ind++;
40         }
41         
42         List<String> res = new ArrayList<>();
43         for(int j = ind + 1; j < n; j++){
44             if(s1Arr[j] == s2Arr[j] || s1Arr[j] != s2Arr[ind]){
45                 continue;
46             }
47             
48             swap(s1Arr, ind, j);
49             res.add(new String(s1Arr));
50             swap(s1Arr, ind, j);
51         }
52         
53         return res;
54     }
55     
56     private void swap(char [] arr, int i, int j){
57         char temp = arr[i];
58         arr[i] = arr[j];
59         arr[j] = temp;
60     }
61 }

 

标签:854,cur,int,s2,s1,ind,Similar,LeetCode,String
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/16634647.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有