当前位置:网站首页>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;
}
}边栏推荐
猜你喜欢

DoS及攻擊方法詳解

vutils. make_ A little experience of grid () in relation to black and white images

带你解决哈希冲突,并实现一个简单hash表,

vutils.make_grid()与黑白图像有关的一个小体会

贝叶斯网络详解

wechat_微信小程序中解决navigator进行页面跳转并传递参数问题

Decision tree and random forest

数字签名论述及生成与优点分析

Detailed explanation of MySQL mvcc mechanism

RSA encryption and decryption details
随机推荐
[QNX] Command
transforms. The input of randomcrop() can only be PIL image, not tensor
[uniapp] the uniapp mobile terminal uses uni Troubleshooting of navigateback failure
Lm06 the mystery of constructing the bottom and top trading strategy only by trading volume
CD-CompactDisk
Bayesian network explanation
RSA加密解密详解
无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!
RSA concept explanation and tool recommendation - LMN
padding百分比操作
LeetCode 128最长连续序列
Detailed explanation of dos and attack methods
Procedure steps for burning a disc
17.13 supplementary knowledge, thread pool discussion, quantity discussion and summary
JVM entry door (1)
ROS的发布消息Publishers和订阅消息Subscribers
Map and list < map > transfer to corresponding objects
非对称密码体制详解
Deep understanding of MySQL lock and transaction isolation level
Concept and working principle of data encryption standard (DES)