当前位置:网站首页>854. String BFS with similarity K
854. String BFS with similarity K
2022-07-05 21:45:00 【Empress Yu】
854. The similarity is K String
character string
s1
ands2
yesk
be similar Of ( For some nonnegative integersk
), If we could exchanges1
The two letters in the are in the right placek
Time , Make the result string equal tos2
.Given two word puzzles
s1
ands2
, returns1
ands2
Andk
be similar Minimumk
.Example 1:
Input :s1 = "ab", s2 = "ba" Output :1Example 2:
Input :s1 = "abc", s2 = "bca" Output :2Tips :
1 <= s1.length <= 20
s2.length == s1.length
s1
ands2
Contains only sets{'a', 'b', 'c', 'd', 'e', 'f'}
Small letters ins2
yess1
A word puzzle
The result of doing the question
Failure ,dfs All the time , Have a headache
Method :BFS
1. The starting steps are 0, Resolve for the sake of seeking a become b The minimum number of steps of
2. Fill in one of the same characters each time , Move to the corresponding position
3. Find it and go back
class Solution {
public int kSimilarity(String s1, String s2) {
Queue<String> queue = new LinkedList<>();
Map<String,Integer> distances = new HashMap<>();
distances.put(s1,0);
queue.offer(s1);
while (!queue.isEmpty()){
String str = queue.poll();
int nextStep = distances.get(str)+1;
for(String next:getNext(str,s2)){
if(next.equals(s2)) return nextStep;
if(!distances.containsKey(next)){
distances.put(next,nextStep);
queue.offer(next);
}
}
}
return 0;
}
private List<String> getNext(String s,String hope){
List<String> ans = new ArrayList<>();
char[] cs1 = s.toCharArray();
char[] cs2 = hope.toCharArray();
int n = cs1.length;
int i = 0;
while (i<n&&cs1[i]==cs2[i]) ++i;
for(int j = i+1; j < n; j++){
if(cs1[j]!=cs2[j]&&cs1[j]==cs2[i]){
swap(cs1,i,j);
ans.add(new String(cs1));
swap(cs1,i,j);
}
}
return ans;
}
private void swap(char[] cs, int i, int j){
char temp = cs[i];
cs[i] = cs[j];
cs[j] = temp;
}
}
边栏推荐
- DBeaver同时执行多条insert into报错处理
- Alibaba cloud award winning experience: build a highly available system with polardb-x
- 办公遇到的问题--
- Teach yourself to train pytorch model to Caffe (III)
- 场景化面试:关于分布式锁的十问十答
- Huawei fast game failed to call the login interface, and returned error code -1
- 华为游戏多媒体服务调用屏蔽指定玩家语音方法,返回错误码3010
- 2.2.3 output of documents
- Huawei game multimedia service calls the method of shielding the voice of the specified player, and the error code 3010 is returned
- 使用Aspect制作全局异常处理类
猜你喜欢
Emotional analysis of wechat chat records on Valentine's day based on Text Mining
2022-07-03-cka- latest feedback from fans
DBeaver同时执行多条insert into报错处理
EasyExcel的读写操作
Golang (1) | from environmental preparation to quick start
怎么利用Tensorflow2进行猫狗分类识别
How can Huawei online match improve the success rate of player matching
总结出现2xx、3xx、4xx、5xx状态码的原因
Deeply convinced plan X - network protocol basic DNS
场景化面试:关于分布式锁的十问十答
随机推荐
EL与JSTL注意事项汇总
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
postgis 安装地理信息扩展
Efficiency difference between row first and column first traversal of mat data types in opencv
Access Zadig self-test environment outside the cluster based on ingress controller (best practice)
Robot framework setting variables
Simple interest mode - lazy type
Detailed explanation of memset() function usage
资深电感厂家告诉你电感什么情况会有噪音电感噪音是比较常见的一种电感故障情况,如果使用的电感出现了噪音大家也不用着急,只需要准确查找分析出什么何原因,其实还是有具体的方法来解决的。作为一家拥有18年品牌
[daily training] 729 My schedule I
Cross end solution to improve development efficiency rapidly
Evolution of zhenai microservice underlying framework from open source component encapsulation to self-development
ESP32
面试官:并发编程实战会吗?(线程控制操作详解)
Deployment of Jenkins under win7
EasyExcel的读写操作
kingbaseES V8R3数据安全案例之---审计记录清除案例
Making global exception handling classes with aspect
他们主动布局(autolayout)环境的图像编辑器
854. 相似度为 K 的字符串 BFS