当前位置:网站首页>Insert string B into string A. how many insertion methods can make the new string a palindrome string
Insert string B into string A. how many insertion methods can make the new string a palindrome string
2022-06-26 18:11:00 【A goblin】
One , Title Description
Palindrome string ” It's a string that is read both forward and backward , such as “level” perhaps “noon” And so on are palindrome strings . Hua Hua likes this palindrome string with symmetrical beauty very much , On her birthday, she got two gifts, one was string A And string B. Now she is very curious about whether there is any way to put the string B Insert string A Make the resulting string a palindrome string . You accept Huahua's request , Help her find out how many ways to insert a new string into a palindrome string . If the string B If the insertion position is different, it will be considered as different methods .
for example :
A = “aba”,B = “b”. Here you are 4 Plant a handle B Insert A The way to :
- stay A Before the first letter of : “baba” Not a palindrome
- In the first letter ‘a’ after : “abba” It's palindrome.
- In the letters ‘b’ after : “abba” It's palindrome.
- In the second letter ’a’ after “abab” Not a palindrome
So the answer is 2
Input description :
Each group has two lines of input data .
The first line is string A
The second behavior string B
String length is less than 100 And contain only lowercase letters
Output description :
Output a number , Represents a string B Insert string A Then the number of methods to form a palindrome string
Two , Their thinking
1, By traversing A, Pass in StringBuilder, And then use StringBuilder Of inset Method to B Insert in
2, Determine whether the new string is a palindrome string
3、 ... and , Implementation code
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String A = scanner.nextLine(); // Input A
String B = scanner.nextLine(); // Input B
int count = 0; // Used to count the number of palindromes
for (int i = 0; i <= A.length(); i++) {
// Traversal string A
// introduce StringBuilder
StringBuilder sb = new StringBuilder(A);
// Use StringBuilder Of insert Method to B Insert A
sb.insert(i, B);
if (isHuiWen(sb.toString())) {
count++; // If it's a palindrome string , Number on +1
}
}
System.out.println(count);
}
private static boolean isHuiWen(String str) {
// Judge whether it is a palindrome string
int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str.charAt((left)) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}边栏推荐
- 【Unity】在Unity中使用C#执行外部文件,如.exe或者.bat
- RuntimeError: CUDA error: out of memory自己的解决方法(情况比较特殊估计对大部分人不适用)
- Applet setting button sharing function
- 图像二值化处理
- Temporarily turn off MySQL cache
- pycharm的plt.show()如何保持不关闭
- Lm06 the mystery of constructing the bottom and top trading strategy only by trading volume
- 比较两个对象的大小关系原来可以如此花里胡哨
- Decision tree and random forest
- 接水面试题
猜你喜欢

wm_concat()和group_concat()函数

MYSQL的下载与配置 mysql远程操控

交叉编译环境出现.so链接文件找不到问题

Solve the problem that each letter occupies a space in pycharm

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

(必须掌握的多线程知识点)认识线程,创建线程,使用Thread的常见方法及属性,以及线程的状态和状态转移的意义

VCD-影音光碟

行锁与隔离级别案例分析

The king of Internet of things protocol: mqtt

深层次安全定义剖析及加密技术
随机推荐
KDD 2022 | how to use comparative learning in cross domain recommendation?
Detailed explanation of dos and attack methods
你好,现在网上股票开户买股票安全吗?
Plt How to keep show() not closed
KDD 2022 | 如何在跨域推荐中使用对比学习?
VCD video disc
Runtimeerror: CUDA error: out of memory own solution (it is estimated that it is not applicable to most people in special circumstances)
Connected to surface test questions
How to open a stock account? Is it safe to open an account online now?
CD-CompactDisk
接水面试题
Number of solutions for knapsack problem
RSA encryption and decryption details
让torch.cuda.is_available()从false变成true的一点经验
ROS查询话题具体内容常用指令
[buuctf.reverse] 126-130
sql中的几种删除操作
Using redis for user access data statistics hyperloglog and bitmap advanced data types
[QNX] Command
新手炒股开户选哪个证券公司比较好?怎样炒股比较安全??