This question already has answers here:
Check string for palindrome
(42 answers)
Closed 2 years ago.
Problem is to check if the string is valid palindrome or not. Return true for empty strings.
What's the problem with my code? It is failing for the input "ab"
public class Solution {
Boolean b;
public Boolean isPalindrome(String s) {
s = s.toLowerCase();
s = s.replaceAll("[^a-zA-Z0-9]","");
if(s.length()==1 || s.isEmpty())
{
return true;
}
for (int i=0;i<s.length()-1;i++)
{
b = expandAroundCenter(s,i,i+1);
b = expandAroundCenter(s,i,i);
}
return b;
}
public Boolean expandAroundCenter(String s,int start,int end)
{
while(start>=0 && end<s.length() )
{
if ((s.charAt(start))!=(s.charAt(end)))
{
return false;
}
start--;
end++;
}
return true;
}
}
You've got big logic flaws here.
Firstly, in your for loop you call expandAroundCenter twice, and overwrite the first results.
Secondly, you're doing this in a for loop, and overwriting all previous results.
Also I think you're making things harder than they need to be by starting in the middle. Start on the edges, and work inward!
Calculating a palindrome is a great opportunity for a recursive function (one that calls itself). I'll give you the pseudo-code, it's up to you to implement:
public Boolean IsPalindrome(string s)
// If we are down to 1 or zero characters, success!
// This deals nicely with odd numbered length strings
if(length <= 1)
return true;
// If the first and last don't match, it's not a palindrome
if(first letter != last letter)
return false;
// Since we know the first and last match, strip them off, then repeat
return IsPalindrome(strip first and last letter from string)
}
If there are no constraints, the best way to solve this problem is to use recursive.
class palindrome
{
static boolean isPalRec(String str,
int s, int e)
{
if(s == "")
return true;
if (s == e)
return true;
if ((str.charAt(s)) != (str.charAt(e)))
return false;
if (s < e + 1)
return isPalRec(str, s + 1, e - 1);
return true;
}
static boolean isPalindrome(String str)
{
int n = str.length();
if (n == 0)
return true;
return isPalRec(str, 0, n - 1);
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
This while code works fine, it is a program to check for palindromes.
public class Solution {
public static boolean checkPalindrome(String str){
int i=0;
int j= str.length()-1;
while(i<j)
{
if(str.charAt(i)!=str.charAt(j))
{
return false;
}
i++;
j--;
}
return true;
}
}
But what will happen in this version, what do you expect the output to be in this?
public class Solution {
public static boolean checkPalindrome(String str){
int i=0;
int j= str.length()-1;
while(i<j)
{
if(str.charAt(i)!=str.charAt(j))
{
return false;
}
else
{
return true;
}
i++;
j--;
}
}
}
In your second code block you are first checking if the first character is the same as the last.
If it isn't, return false.
Else, return true.
So this isn't going to be a valid palindrome check. It only checks if the first and last letters are the same or not and ignores the letters in the middle.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Problem in infix to postfix conversion
at the point when value fetched is '-' then the compiler does not enter at else-if block (last case)
package com.conversion;
import java.util.Scanner;
public class mainclass {
private static Scanner sc;
public static void main(String[] args) {
// TODO Auto-generated method stub
String s;
sc = new Scanner(System.in);
System.out.println("Enter a complete expresion");
s= sc.next();
stackop ob = new stackop();
System.out.println("YOUR PROVIDED input expression is :"+s);
//System.out.print(" Equivalent postfix notation is : ");
System.out.println("\n\n");
ob.postfix(s);
}
}
class stackop{
char stack1[]=new char[50];
int top;
void push(char ch)
{
top++;
stack1[top]=ch;
System.out.println(" element pushed is: "+ch);
System.out.println("----- current value of top is after push overs : "+top+" -------");
}
char pop(){
char ch;
ch= stack1[top];
//System.out.println("current value of top is : "+top);
System.out.println(" element popped is: "+ch);
--top;
System.out.println("----- current value of top is after pop overs : "+top+" -------");
return ch;
}
boolean isalpha(char a)
{
if((a>='a' && a<='z')||(a>=0 && a<=9))
return true;
else
return false;
}
boolean operator(char ch)
{
if(ch=='/'||ch=='*'||ch=='+'||ch=='-')
return true;
else
return false;
}
int prech(char ch){
switch(ch)
{
case '-': return 1;
//break;
case '+': return 1;
//break;
case '/': return 2;
//break;
case '*': return 2;
}
return 0;
}
void postfix(String str)
{
char otop=0;
char[] output = new char[str.length()+1];
for(int i=0 ; i<str.length();i++)
{
char ch=str.charAt(i);
System.out.println("==========value fetched is "+ch+" ===========");
if(ch=='(')
push(ch);
else if(isalpha(ch))
{
System.out.println("elemnt inserted in output list is :"+ch);
output[++otop]=ch;
}
else if(ch == ')' )
{
char temp=0;
System.out.println(" a close bracket is encounterd ");
while((temp=pop())!='(')
{
output[++otop]=temp;
System.out.println("elemnt inserted in output list is :"+temp);
}
}
else if(operator(ch))
{
if(prech(stack1[top])==0||(prech(stack1[top])<=prech(ch))||(prech(stack1[top])=='('))
push(ch);
}
else if(prech(stack1[top]) > prech(ch))
{
System.out.println(" hahah here i come");
output[++otop]=pop();
push(ch);
}
}
while(top>0)
{
output[++otop]=pop();
System.out.println("elemnt inserted in output list is :"+output[otop]);
}
System.out.println("\n\nOUTPUT OF EXPRESSION IS : " );
for(int j=0;j<str.length();j++)
{
System.out.print(""+output[j]);
}
}
}
In this chunk of code:
else if(operator(ch))
{
if(prech(stack1[top])==0||(prech(stack1[top])<=prech(ch))||(prech(stack1[top])=='('))
push(ch);
}
else if(prech(stack1[top]) > prech(ch))
The operator(ch) method returns true for your operators, which means control flow never reaches the second else if. What you probably want to do is move it inside the first else if, as shown below:
else if(operator(ch))
{
if(prech(stack1[top])==0||(prech(stack1[top])<=prech(ch))||(prech(stack1[top])=='('))
{
push(ch);
}
else if(prech(stack1[top]) > prech(ch)) { ... }
}
The title and the question do not have anything in common, also you should format your code. Nonetheless here's my answer to the question which you probably would've asked had you written an appropriate title.
Well actually 0 != '0' in programming, because '0' is translated to it's integer value which in ASCII is 48 and the comparison is between 0 and 48.
So your isalpha method should look like this:
boolean isalpha(char a)
{
if((a>='a' && a<='z')||(a>='0' && a<='9'))
return true;
else
return false;
}
Also, there are these neat little methods with which you don't need to check characters' ASCII code to compare them. I personally prefer this approachh as it is more simple and concise. It would look like this:
boolean isalpha(char a)
{
return Character.isDigit(a) || Character.isAlphabetic(a);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Here i used this code for generating one password which should contain a uppercase,a lowercase and a number and a special char. this is my code for checking.
String randomPassword = genRanPass();
boolean hasUppercase = !randomPassword.equals(randomPassword.toLowerCase());
boolean hasLowercase = !randomPassword.equals(randomPassword.toUpperCase());
boolean hasNumber = randomPassword.matches(".*\\d.*");
if(!(randomPassword.contains("!") || randomPassword.contains("#") || randomPassword.contains("#") || randomPassword.contains("$") || randomPassword.contains("_") || randomPassword.contains("-"))){
while(!hasUppercase && hasLowercase && hasNumber)
{
randomPassword = genRanPass();
}
}else{
while(!hasUppercase && hasLowercase && hasNumber)
{
randomPassword = genRanPass();
}
return randomPassword;
}
genRanPass(); generating a password having random number,a spcial char,a letter Uppercase and lower case
but i got hnga$VVj from my method and when i am checking
while(!hasUppercase && hasLowercase && hasNumber)
{
randomPassword = genRanPass();
}
return randomPassword;
while(!hasUppercase && hasLowercase && hasNumber) its became true,true and false,its not going inside this method plase help me.
{
I strongly recommend you use a straight forward for-each loop and the utility methods from Character. Something like,
public static boolean isPasswordValid(String password) {
if (password == null) return false;
boolean hasUpper = false;
boolean hasLower = false;
boolean hasNumber = false;
boolean hasSpecial = false;
for (char ch : password.toCharArray()) {
if (Character.isUpperCase(ch)) {
hasUpper = true;
} else if (Character.isLowerCase(ch)) {
hasLower = true;
} else if (Character.isDigit(ch)) {
hasNumber = true;
} else {
hasSpecial = true;
}
}
return hasUpper && hasLower && hasNumber && hasSpecial;
}
I am working a sudoku solver and am having trouble returning / ending the solver function correctly. The show() in the moveOn() function gets called and it displays the compleated sudoku fine, however solve returns false. I am trying to have solve return true when the problem is solved and null when it is unsolveable however do not know how to accomplish this.
L is the length of the board (a 9 x 9 sudoku would have L = 9)
getSquare(r,c) returns the value in a 2 dimensional array representing the sudoku board
the different check functions check to see if a value can fit in a specific location. They are not the issue.
the show() function prints out the array in console so it looks like a proper sudoku board.
I also have an isSolved() function that checks the 2D array and if it is a valid solved sudoku returns true, otherweise returns false. I have attempted to implement this as well into the solve() method hoping to use that to return true, though have had no success
//This method's only purpose it to call the findNum function on the next location in the sudoku
public void moveOn(int row, int column) {
//if the previous location was not the last in the row move to ne next cell in said row.
//if it was the last location in the row, move to the first column of the next row
if (column + 1 != L) {solve(row, column + 1);}
else if (row + 1 != L) {solve(row + 1, 0);}
else {show();}
}
//This method finds a valid number for a specific location on the sudoku grid\
public boolean solve(int row, int column) {
if (row >= L) {return true;}
//pass over any numbers that are not empty
if (getSquare(row, column) != 0) {moveOn(row, column);}
else {
//attempt to find a valid number for the location
for (int n = 1; n <= L; n++) {
if (checkRow(row, n) && checkCol(column, n) && checkSquare(row, column, n)) {
// If a number is allowed at a specific location set that location to the number
setSquare(row, column, n);
//Begin checking for a solution based on previous numbers changed
moveOn(row, column);
}
}
//If no number is allowed in this space backtrack to the last successful number
//changed and reset all locations that have been changed recursively
setSquare(row, column, 0);
}
//If the puzzle is unsolveable
return false;
}
Many thanks to anybody that can help shed some light on the situation.
If more of my code / information is needed I will gladly provide
Sample input file: http://pastebin.com/6mSKT3ES
Edit: complete code removed
You have only one return statement in the solve function, and that is
return false;
and since that is the last statement in the function, and unconditionally executed, solve will, unless an exception is thrown, always return false.
To get a return value that actually tells you whether you found a solution, you need to make the return value depend on a condition. Also, once you have found a solution, for well-posed puzzles, there is no point in continuing to search.
So you should add a conditional return true; in the searching loop. For that, you need to know when you have found a solution. You wrap the recursion in an intermediate call to moveOn, so the simplest change would be to add a return value to moveOn:
public boolean moveOn(int row, int column) {
//if the previous location was not the last in the row move to ne next cell in said row.
//if it was the last location in the row, move to the first column of the next row
if (column + 1 != L) {return solve(row, column + 1);}
else if (row + 1 != L) {return solve(row + 1, 0);}
else {show(); return true;} // reached end of grid, solved
}
and use that in `solve':
public boolean solve(int row, int column) {
//pass over any numbers that are not empty
if (getSquare(row, column) != 0) {return moveOn(row, column);}
else {
//attempt to find a valid number for the location
for (int n = 1; n <= L; n++) {
if (checkRow(row, n) && checkCol(column, n) && checkSquare(row, column, n)) {
// If a number is allowed at a specific location set that location to the number
setSquare(row, column, n);
//Begin checking for a solution based on previous numbers changed
if (moveOn(row, column)) {
return true; // solved, yay!
}
}
}
//If no number is allowed in this space backtrack to the last successful number
//changed and reset all locations that have been changed recursively
setSquare(row, column, 0);
}
//If the puzzle is unsolveable
return false;
}
This code works...
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class Sudoku {
public static int suRows=9;
public static int suColumns=9;
public static int [][] suArray = new int[suRows][suColumns];
public static int [] dummyRowArray = new int[suRows];
public static int [] dummyColArray = new int[suColumns];
public static int [][] dummy3x3Array = new int[3][3];
public static int [] dummyArray = new int[9];
//read the contents of the file into a 2 dimensional array
public static void readSudoku() throws FileNotFoundException, IOException
{
System.out.print("Please enter the full file name containing the Sudoku Puzzle (e.g:X:/sudoku_case1.txt):");
Scanner scan = new Scanner (System.in);
String filename = scan.nextLine();
File file = new File( filename );
if ( file.exists() )
{
Scanner inFile = new Scanner( file );
for(int i=0; i<suRows; i++)
{
for(int j=0; j<suColumns; j++)
{
if(inFile.hasNextInt()) suArray[i][j] = inFile.nextInt();
}
}
inFile.close();
}
}
public static boolean inOrder(int [] a, int i, int j)
{
return a[i]<=a[j];
}
public static void swap(int [] a, int i, int j)
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
public static void exchangeSort(int [] a)
{
for(int i=0;i<a.length;i++)
{
for(int j=1;j<a.length-i;j++)
{
if(!inOrder(a,j-1,j)) swap(a,j-1,j);
}
}
}
public static void displaySudoku(int [][] suArray)
{
for(int i=0; i<suArray.length;i++)
{
for(int j=0; j<suArray[i].length;j++)
{
System.out.print(suArray[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
//Check if there are any more zeros
public static boolean isComplete(int [][]suArray)
{
boolean result=true;
//Check every element for 0
for(int i=0; i<suArray[0].length; i++)
{
for(int j=0 ; j<suRows ; j++)
{
if(suArray[i][j]==0) return false;
}
}
System.out.println("There are no zeros in this Sudoku");
return result;
}
//Check for adjacent duplicates
public static boolean isAdjacentDup(int [] a)
{
for(int i=1; i<a.length; i++)
{
if((a[i-1]!=0 || a[i]!=0))
{
if(a[i-1]==a[i]) return true;
}
}
return false;
}
//Check for 1 through 9
public static boolean is1to9(int [] a)
{
int j=1;
for(int i=0; i<a.length; i++)
{
if(a[i]==j) j++;
else return false;
}
return true;
}
public static boolean isDuplicate(int [][]suArray)
{
boolean result=false;
//Check every row for duplicates
for(int i=0; i<suArray[0].length; i++)
{
for(int j=0 ; j<suRows ; j++)
{
dummyRowArray[j]=suArray[i][j];
}
//Sort dummyArray so that duplicates can be checked
exchangeSort(dummyRowArray);
//Now check Adjacent elements for duplicates
if(isAdjacentDup(dummyRowArray)==true)
{
System.out.println("Duplicates are found in row");
return true;
}
}
displaySudoku(suArray);
//Check every column for duplicates
for(int j=0; j<suArray[0].length; j++)
{
for(int i=0 ; i<suColumns ; i++)
{
dummyColArray[i]=suArray[i][j];
}
//Sort dummyArray so that duplicates can be checked
exchangeSort(dummyColArray);
//Now check Adjacent elements for duplicates
if(isAdjacentDup(dummyColArray)==true)
{
System.out.println("Duplicates are found in columns");
return true;
}
}
displaySudoku(suArray);
System.out.println("3X3:");
//Check every 3X3 matrix for duplicates
// 1st block
if(is3x3Duplicate(suArray,0,3,0,3)==true)
{
System.out.println("1st Block has a duplicate");
return true;
}
else System.out.println("1st Block has no duplicate");
// 2nd block
if(is3x3Duplicate(suArray,0,3,3,6)==true)
{
System.out.println("2nd Block has a duplicate");
return true;
}
// 3rd block
if(is3x3Duplicate(suArray,0,3,6,9)==true)
{
System.out.println("3rd Block has a duplicate");
return true;
}
// 4th block
if(is3x3Duplicate(suArray,3,6,0,3)==true)
{
System.out.println("4th Block has a duplicate");
return true;
}
// 5th block
if(is3x3Duplicate(suArray,3,6,3,6)==true)
{
System.out.println("5th Block has a duplicate");
return true;
}
// 6th block
if(is3x3Duplicate(suArray,3,6,6,9)==true)
{
System.out.println("6th Block has a duplicate");
return true;
}
// 7th block
if(is3x3Duplicate(suArray,6,9,0,3)==true)
{
System.out.println("7th Block has a duplicate");
return true;
}
// 8th block
if(is3x3Duplicate(suArray,6,9,3,6)==true)
{
System.out.println("8th Block has a duplicate");
return true;
}
// 9th block
if(is3x3Duplicate(suArray,6,9,6,9)==true)
{
System.out.println("9th Block has a duplicate");
return true;
}
return result;
}
//Check every3X3 grid for duplicates
public static boolean is3x3Duplicate(int [][]suArray, int x1, int x2, int y1, int y2)
{
boolean result=false;
int k=0, l=0;
for(int i=x1; i<x2;i++)
{
for(int j=y1;j<y2;j++)
{
dummy3x3Array[k][l]=suArray[i][j];
l++;
}
l=0;
k++;
}
displaySudoku(dummy3x3Array);
for(int i=0; i<dummy3x3Array.length; i++)
{
for(int j=0; j<dummy3x3Array.length; j++)
{
dummyArray[(i*dummy3x3Array.length) + j] = dummy3x3Array[i][j];
}
}
System.out.println(Arrays.toString(dummyArray));
//Sort dummyArray so that duplicates can be checked
exchangeSort(dummyArray);
//Now check Adjacent elements for duplicates
if(isAdjacentDup(dummyArray)==true)
{
System.out.println("Duplicates are found in blocks");
return true;
}
return result;
}
//Check every3X3 grid for 1 trough 9
public static boolean is3x3have1to9(int [][]suArray, int x1, int x2, int y1, int y2)
{
boolean result=false;
int k=0, l=0;
for(int i=x1; i<x2;i++)
{
for(int j=y1;j<y2;j++)
{
dummy3x3Array[k][l]=suArray[i][j];
l++;
}
l=0;
k++;
}
for(int i=0; i<dummy3x3Array.length; i++)
{
for(int j=0; j<dummy3x3Array.length; j++)
{
dummyArray[(i*dummy3x3Array.length) + j] = dummy3x3Array[i][j];
}
}
//Sort dummyArray so that duplicates can be checked
exchangeSort(dummyArray);
//Now check Adjacent elements for duplicates
if(is1to9(dummyArray)==false)
{
System.out.println("Block doe snot have 1 through 9");
return false;
}
return result;
}
public static boolean isValidSudoku(int [][] suArray)
{
// boolean result=true;
//All nos should be between 0 and 9
for(int i=0; i<suArray.length; i++)
{
for(int j=0; j<suArray[i].length; j++)
{
if(suArray[i][j]<0 || suArray[i][j]>9){
System.out.println("Nos in the puzzle are out of range");
return false;
}
}
}
//Call teh isDuplicate function to check for duplicates in the rows
if( isDuplicate(suArray)==true) return false;
return true;
}
public static boolean isSolvedSudoku(int [][] suArray)
{
//Check every row for 1 to 9
for(int i=0; i<suArray[0].length; i++)
{
for(int j=0 ; j<suRows ; j++)
{
dummyRowArray[j]=suArray[i][j];
}
//Sort dummyArray so that duplicates can be checked
exchangeSort(dummyRowArray);
if(is1to9(dummyRowArray)==false)
{
System.out.println("1 through 9 not present in rows");
return false;
}
}
//Check every column for 1 to 9
for(int j=0; j<suArray[0].length; j++)
{
for(int i=0 ; i<suColumns ; i++)
{
dummyColArray[i]=suArray[i][j];
}
//Sort dummyArray so that duplicates can be checked
exchangeSort(dummyColArray);
//Now check Adjacent elements for duplicates
if(is1to9(dummyColArray)==false)
{
System.out.println("1 through 9 not present in columns");
return false;
}
}
//Check every 3X3 matrix for 1 through 9
// 1st block
if(is3x3have1to9(suArray,0,3,0,3)==true)
{
System.out.println("1st Block does not contain 1 through 9");
return false;
}
// 2nd block
if(is3x3have1to9(suArray,0,3,3,6)==true)
{
System.out.println("2nd Block does not contain 1 through 9");
return false;
}
// 3rd block
if(is3x3have1to9(suArray,0,3,6,9)==true)
{
System.out.println("3rd Block does not contain 1 through 9");
return false;
}
// 4th block
if(is3x3have1to9(suArray,3,6,0,3)==true)
{
System.out.println("4th Block does not contain 1 through 9");
return false;
}
// 5th block
if(is3x3have1to9(suArray,3,6,3,6)==true)
{
System.out.println("5th Block does not contain 1 through 9");
return false;
}
// 6th block
if(is3x3have1to9(suArray,3,6,6,9)==true)
{
System.out.println("6th Block does not contain 1 through 9");
return false;
}
// 7th block
if(is3x3have1to9(suArray,6,9,0,3)==true)
{
System.out.println("7th Block does not contain 1 through 9");
return false;
}
// 8th block
if(is3x3have1to9(suArray,6,9,3,6)==true)
{
System.out.println("8th Block does not contain 1 through 9");
return false;
}
// 9th block
if(is3x3have1to9(suArray,6,9,6,9)==true)
{
System.out.println("9th Block does not contain 1 through 9");
return false;
}
return true;
}
public static boolean solveSudoku(int [][] s)
{
//Check if Valid
if(isValidSudoku(suArray)==true) System.out.println("Sudoku is valid");
else
{
System.out.println("Not valid!");
return false;
}
//Check if Complete - No 0's in any place
if(isComplete(suArray)==true)
{
System.out.println("Sudoku is Complete");
return true;
}
else System.out.println("Sudoku is not Complete");
//Check if solved - 1-9 present in every row, column and 3x3
if(isSolvedSudoku(suArray)==true) System.out.println("Sudoku is Solved!");
else System.out.println("Not Solved");
//Locate the first 0 in the Sudoko puzzle
for(int i=0; i<suArray.length; i++)
{
for(int j=0 ; j<suArray[i].length ; j++)
{
if(suArray[i][j]==0)
{
System.out.println("0 found in location: " + i +" " + j );
for(int k=1;k<10;k++)
{
suArray[i][j]=k;
System.out.println("New value assigned to Sudoku: " + k);
displaySudoku(suArray);
if(solveSudoku(suArray))// isValidSudoku(suArray))
{
displaySudoku(suArray);
System.out.println("New value assigned to Sudoku");
return true;
}
}
suArray[i][j]=0;
return false; // remove this
}
}
}
return true;
}
public static void main(String[] args) throws FileNotFoundException, IOException {
readSudoku();
displaySudoku(suArray);
if( (isValidSudoku(suArray)!=true))
{
System.out.println("The given Sudoku Puzzle is not Valid!. Exiting....");
System.exit(0);
}
do
{
if(isSolvedSudoku(suArray)==true)
{
System.out.println("Sudoku is Completely Solved!");
}
if(solveSudoku(suArray)==true)
{
System.out.println("Sudoku is now solved");
}
else System.out.println("Not Complete");
}while(isSolvedSudoku(suArray)!=true);
System.out.println("Sudoku is now completely solved:");
displaySudoku(suArray);
}
}