当前位置:网站首页>将字符串B插入字符串A,有多少种插入办法可以使新串是一个回文串
将字符串B插入字符串A,有多少种插入办法可以使新串是一个回文串
2022-06-26 18:01:00 【一只懐坏旭】
一,题目描述
回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。花花非常喜欢这种拥有对称美的回文串,生日的时候她得到两个礼物分别是字符串A和字符串B。现在她非常好奇有没有办法将字符串B插入字符串A使产生的字符串是一个回文串。你接受花花的请求,帮助她寻找有多少种插入办法可以使新串是一个回文串。如果字符串B插入的位置不同就考虑为不一样的办法。
例如:
A = “aba”,B = “b”。这里有4种把B插入A的办法:
- 在A的第一个字母之前: “baba” 不是回文
- 在第一个字母‘a’之后: “abba” 是回文
- 在字母‘b’之后: “abba” 是回文
- 在第二个字母’a’之后 “abab” 不是回文
所以满足条件的答案就是2
输入描述:
每组输入数据共两行。
第一行为字符串A
第二行为字符串B
字符串长度均小于100且只包含小写字母
输出描述:
输出一个数字,表示把字符串B插入字符串A之后构成一个回文串的方法数
二,解题思路
1,通过遍历A,传入StringBuilder,然后用StringBuilder的inset方法把B插入进去
2,判断新的字符串是否为回文串
三,实现代码
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String A = scanner.nextLine(); //输入A
String B = scanner.nextLine(); //输入B
int count = 0; //用来统计回文串数目
for (int i = 0; i <= A.length(); i++) {
//遍历字符串A
//引入StringBuilder
StringBuilder sb = new StringBuilder(A);
//使用StringBuilder的insert方法把B插入A
sb.insert(i, B);
if (isHuiWen(sb.toString())) {
count++; //如果是回文串,数目就+1
}
}
System.out.println(count);
}
private static boolean isHuiWen(String str) {
//判断是否为回文串
int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str.charAt((left)) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}边栏推荐
- Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!
- KDD 2022 | 如何在跨域推荐中使用对比学习?
- 【Unity】在Unity中使用C#执行外部文件,如.exe或者.bat
- 17.13 supplementary knowledge, thread pool discussion, quantity discussion and summary
- Ethereum技术架构介绍
- 小程序设置按钮分享功能
- Digital signature standard (DSS)
- Runtimeerror: CUDA error: out of memory own solution (it is estimated that it is not applicable to most people in special circumstances)
- 陈强:阿里千亿级大规模数字商业知识图谱助力业务增长
- How sparksql returns a specific day of the week by date -dayofweek function
猜你喜欢

Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!

LM06丨仅用成交量构造抄底摸顶策略的奥秘

一起备战蓝桥杯与CCF-CSP之大模拟炉石传说

Daily record 2

mysql Add column 失败 因为之前有数据,不是默认null 不行

wechat_ Solve the problem of page Jump and parameter transfer by navigator in wechat applet

Detailed explanation of dos and attack methods

#26class中get和set设置
![[uniapp] the uniapp mobile terminal uses uni Troubleshooting of navigateback failure](/img/26/da00e70d0955bcdef3362474bc5fc7.png)
[uniapp] the uniapp mobile terminal uses uni Troubleshooting of navigateback failure

transforms.RandomCrop()的输入只能是PIL image 不能是tensor
随机推荐
transforms.RandomCrop()的输入只能是PIL image 不能是tensor
一起备战蓝桥杯与CCF-CSP之大模拟炉石传说
Comp281 explanation
【QNX】命令
Dos et détails de la méthode d'attaque
How does Guosen Securities open an account? Is it safe to open a stock account through the link
Several key points in divorce agreement
行锁分析和死锁
Number of solutions for knapsack problem
14 MySQL tutorial insert insert data
Lm06 the mystery of constructing the bottom and top trading strategy only by trading volume
How to open a stock account? Is it safe to open an account online now?
Tencent qianzhiming: Exploration and application of pre training methods in information flow business
IDEA收藏代码、快速打开favorites收藏窗口
非对称密码体制详解
Use FST JSON to automatically generate faster JSON serialization methods
【uniapp】uniapp手机端使用uni.navigateBack失效问题解决
Detailed explanation of dos and attack methods
Vscode usage - Remote SSH configuration description
决策树与随机森林