[英]Check whether a given String is palindrome or not without using any library without loop
I was asked in an interview to write code to check if a given string is a palindrome or can be a palindrome by altering some character without using a library function.在一次采访中,我被要求编写代码来检查给定的字符串是回文还是可以通过在不使用库函数的情况下更改某些字符来成为回文。 Here is my Approach
这是我的方法
import java.util.Scanner;
public class Palindrom {
static int temp=0;
static char[] cArr;
static boolean chackPotentialPalindrom(char[] cAr){
cArr=cAr;
if(cArr!=null){
char current=cArr[0];
for(int i=1;i<cArr.length;i++){
if(current==cArr[i]){
cArr=removeElement(i);
chackPotentialPalindrom(cArr);
break;
}
}
if(cAr.length==2){
if(cAr[0]==cAr[1]){
cArr=null;
}}
if(temp==0 && cArr!=null){
temp=1;
cArr=removeFirstElement(cArr);
chackPotentialPalindrom(cArr);
}
}
if(cArr==null){
return true;
}else{
return false;
}
}
static char[] removeFirstElement(char[] cAr){
cArr=cAr;
if(cArr!=null){
if(cArr.length >1){
char[] cArrnew=new char[cArr.length-1];
for(int j=1,k=0;j<cArr.length;j++,k++){
cArrnew[k]=cArr[j];
}
return cArrnew;
} else {
return null;
}
} else {
return null;
}
}
static char[] removeElement(int i){
if(cArr.length>2){
char[] cArrnew=new char[cArr.length-2];
for(int j=1,k=0;j<cArr.length;j++,k++){
if(i!=j){
cArrnew[k]=cArr[j];
}else{
k-=1;
}
}
return cArrnew;}
else{
return null;
}
}
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
while(true){
temp=0;
String s=scn.next();
char[] arr=s.toCharArray();
System.out.println(chackPotentialPalindrom(arr));
}
}
}
Any tips to optimize this code?I could not write this in an interview as they have given a pen and paper to code.It took 3 hrs for me to write this.优化此代码的任何提示?我无法在采访中写这个,因为他们已经给了笔和纸来编写代码。我花了 3 个小时来写这个。 Can I be a developer?
我可以成为开发人员吗?
Title says "without loop" but you need to check all symbol pairs, so using recursion, as you have tried, looks reasonable.标题说“没有循环”,但您需要检查所有符号对,因此正如您所尝试的那样,使用递归看起来很合理。 But you don't check and use results of recursive calls.
但是您不会检查和使用递归调用的结果。
Pseudocode might look like (note we don't need to change source data or extract substring):伪代码可能看起来像(注意我们不需要更改源数据或提取子字符串):
Edit to provide possibility to alter one char编辑以提供更改一个字符的可能性
boolean checkPotentialPalindrom(char[] cAr, start, end, altcnt){
if (end <= start)
return true
if (cAr[start] != cAr[end])
altcnt = altcnt + 1
if (altcnt > 1)
return false
return checkPotentialPalindrom(cAr, start + 1, end - 1, altcnt)
}
and make the first call with arguments 0, len(cAr-1), 0
并使用参数
0, len(cAr-1), 0
进行第一次调用
Answering to your first question..You have to use recursion to solve this problem.回答你的第一个问题..你必须使用递归来解决这个问题。 Here is my approach.
这是我的方法。
public boolean isPalindrom(char[] str, int start, int end) {
if (end <= start)
return true;
if (str[start] != str[end] || arraySize(str) <= 1)
return false;
return isPalindrom(str, start + 1, end - 1);
}
public int arraySize(char[] str) {
int count = 0;
for (char i : str) {
count++;
}
return count;
}
You have tried to implement this algorithm using loops and you can simplify it like this您已尝试使用循环来实现此算法,您可以像这样简化它
public boolean isPalindroms(char[] str) {
int diffCount = 0;
int left = 0;
int right = arraySize(str) - 1;
while (right > left) {
if (str[right--] != str[left++]) {
diffCount++;
}
}
return (diffCount < 2);
}
public int arraySize(char[] str) {
int count = 0;
for (char i : str) {
count++;
}
return count;
}
The answer for the second question that you have ask is definitely you can be a good developer.您提出的第二个问题的答案肯定是您可以成为一名优秀的开发人员。 Computer programming is a Craft.
计算机编程是一门手艺。 Not Some Kind of Rocket Science.
不是某种火箭科学。 You have to master it by crafting it.
你必须通过制作它来掌握它。
using recursive function使用递归函数
calling with left=0 and right = arr.length-1
用
left=0 and right = arr.length-1
调用
public static boolean isPalindrom(char[] arr, int left, int right){
if(arr[left++] != arr[right--])
return false;
if(left < right)
isPalindromss(arr, left++, right--);
return true;
}
if you have to use while loop, you can simplify it like following如果你必须使用 while 循环,你可以像下面这样简化它
public boolean isPalindrom(char[] arr){
int left=0;
int right = arr.length-1;
while(left < right){
if(arr[left++] == arr[right--])
continue;
return false;
}
return true;
}
Using StringBuilder
, We can do it使用
StringBuilder
,我们可以做到
public static boolean isPalindrom(String str, int len){
StringBuilder sb= new StringBuilder(str);
if((len > 1) & !(sb.substring(0,len/2 + 1).equals(sb.reverse().substring(0,len/2 + 1))))
return false;
return true;
}
function palin(input, leftIdx = 0, rightIdx = input.length - 1) { if (leftIdx >= rightIdx) return true; if (input[leftIdx] != input[rightIdx]) return false; return palin(input, leftIdx + 1, rightIdx - 1); } const testCases = { air: false, airia: true, caria: false, a: true, bb: true, bcdb: false, zzaaaaz: false, }; Object.keys(testCases).forEach(test => console.log("Test: ", test, " is palindrome: ", palin(test), testCases[test]) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.