当前位置:网站首页>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
s1ands2yeskbe similar Of ( For some nonnegative integersk), If we could exchanges1The two letters in the are in the right placekTime , Make the result string equal tos2.Given two word puzzles
s1ands2, returns1ands2Andkbe similar Minimumk.Example 1:
Input :s1 = "ab", s2 = "ba" Output :1Example 2:
Input :s1 = "abc", s2 = "bca" Output :2Tips :
1 <= s1.length <= 20s2.length == s1.lengths1ands2Contains only sets{'a', 'b', 'c', 'd', 'e', 'f'}Small letters ins2yess1A 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;
}
}边栏推荐
- R language learning notes
- Huawei cloud modelarts text classification - takeout comments
- Some common processing problems of structural equation model Amos software
- 阿里云有奖体验:用PolarDB-X搭建一个高可用系统
- Defect detection - Halcon surface scratch detection
- Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
- Deeply convinced plan X - network protocol basic DNS
- Cold violence -- another perspective of objective function setting
- Problems encountered in office--
- kingbaseES V8R3数据安全案例之---审计记录清除案例
猜你喜欢

R language learning notes

Recursive query of multi-level menu data

Deployment of Jenkins under win7

What should I do to prepare for the interview algorithm position during school recruitment?

uni-app 蓝牙通信

Parker driver maintenance COMPAX controller maintenance cpx0200h

Zhang Lijun: penetrating uncertainty depends on four "invariants"

KingbaseES V8R3集群维护案例之---在线添加备库管理节点

PIP install beatifulsoup4 installation failed

2022-07-03-CKA-粉丝反馈最新情况
随机推荐
2022-07-03-cka- latest feedback from fans
Selenium finds the contents of B or P Tags
crm创建基于fetch自己的自定义报告
Yolov5 training custom data set (pycharm ultra detailed version)
Ethereum ETH的奖励机制
Some common processing problems of structural equation model Amos software
Reading and writing operations of easyexcel
selenium 获取dom内属性值的方法
Efficiency difference between row first and column first traversal of mat data types in opencv
selenium 获取dom内验证码图片
Zhang Lijun: penetrating uncertainty depends on four "invariants"
华为快游戏调用登录接口失败,返回错误码 -1
Haas506 2.0 development tutorial - Alibaba cloud OTA - PAC firmware upgrade (only supports versions above 2.2)
EasyExcel的读写操作
Simple interest mode - evil Chinese style
postgis 安装地理信息扩展
NET中小型企业项目开发框架系列(一个)
EasyExcel的讀寫操作
他们主动布局(autolayout)环境的图像编辑器
阿里云有奖体验:用PolarDB-X搭建一个高可用系统