I am having trouble rewriting the following code as a recursive method rather than using the for loop. The for loop tests to see if the String 'noSpaces' is a palindrome (the same forwards as it is backwards). The noSpaces String has no punctuation, spaces, or differences in capitalization.
Thanks for the help
public boolean isRegularPalindrome(String noSpaces) {
noSpaces = noSpaces.toUpperCase();
String[] letters = new String[noSpaces.length()];
for (int i = 0; i < letters.length; i++) {
letters[i] = Character.toString(noSpaces.charAt(i));
}
for (int i = 0; i < letters.length / 2; i++) {
if (!letters[i].equals(letters[letters.length - i - 1])) {
return false;
}
}
return true;
}
There you go:
public static boolean isPalindrome(String input) {
if (input.charAt(0) != input.charAt(input.length() - 1)) {
// Missmatch. Not a palindrome!
return false;
} else if (input.length() > 1){
// If there is more to test, continue.
return isPalindrome(input.substring(1, input.length() - 1));
} else {
// All chars were tested, or 1 char input. Palindrome!
return true;
}
}
Writing a recursive algorithm for anything requires base cases. For a palindrome, this would be a string of length 0 or length 1 -- if the string is length 0 or 1, it is a palindrome.
If the base cases aren't met, you check the first character against the last character.
If the characters aren't the same, return false.
If the characters are the same, return the recursive call to the string except for the first and last characters.
The code should look something like this.
public boolean isPalindrome(string str){
if (str.length == 0)
return true;
else if (str.length == 1)
return true;
else if(str.charAt(0) != str.charAt(str.length - 1)
return false;
else
return isPalindrome(str.substring(1, length - 1));
}
Related
There is something wrong with my code as one the testcase in my assignment is coming out wrong, giving me runtime error when I submit the code online. That testcase could be any String. I believe that everything is fine with the code as I have checked it manually for many testcases.
HERE IS THE CODE
public static boolean isStringPalindrome(String input) {
if(input.length()==0 || input.length()==1)
return true;
int first = 0;
int last = input.length()-1;
if(input.charAt(first) != input.charAt(last))
return false;
String str="";
for(int i=first+1;i<last;i++){
str = str+input.charAt(i);
}
boolean sa = isStringPalindrome(str);
return sa;
}
Sample Input
racecar
Output
true
Sample Input
pablo
Output
false
Your code appears to be overly complicated for recursively testing if the String is a palindrome. Something like,
public static boolean isStringPalindrome(String input) {
if (input == null) {
return false;
} else if (input.isEmpty() || input.length() == 1) {
return true;
}
int len = input.length() - 1;
return input.charAt(0) == input.charAt(len) //
&& isStringPalindrome(input.substring(1, len));
}
Is recursive without embedding a for loop. Because if you can do that, you should do something like
public static boolean isStringPalindrome(String input) {
if (input == null) {
return false;
} else if (input.isEmpty() || input.length() == 1) {
return true;
}
int len = input.length();
for (int i = 0; i <= len / 2; i++) {
if (input.charAt(i) != input.charAt(len - 1 - i)) {
return false;
}
}
return true;
}
A simpler way to check for palindrome can be:
public static boolean isPalindrome(String s)
{ if (input == null)
return false;
else if(s.length() == 0 || s.length() == 1)
return true;
/* check for first and last char of String:
* if they are same then do the same thing for a substring
* with first and last char removed. and carry on this
* until you string completes or condition fails.
*/
if(s.charAt(0) == s.charAt(s.length()-1))
return isPalindrome(s.substring(1, s.length()-1));
return false;
}
Update
You are getting runtime error(NZEC) which means non-zero exit code. It means your program is ending unexpectedly. I don't see any reason except that your program doesn't have a null check. Otherwise, I have gone through your code carefully, you are doing the same thing which I have suggested.
What I have so far:
public boolean allSameLetter(String str)
{
for (int i = 1; i < str.length(); i++)
{
int charb4 = i--;
if ( str.charAt(i) != str.charAt(charb4))
{
return false;
}
if ( i == str.length())
{
return true;
}
}
}
Please excuse any inefficiencies if any; still relatively new to coding in general. Am I lacking some knowledge in terms of using operators and .charAt() together? Is it illogical? Or is my error elsewhere?
Using regex:
return str.matches("^(.)\\1*$");
Using streams:
str.chars().allMatch(c -> c == str.charAt(0));
Other:
return str.replace(String.valueOf(str.charAt(0), "").length() == 0;
You can follow the below steps:
(1) Get the first character (i.e., 0th index)
(2) Check the first character is the same with subsequent characters, if not return false (and comes out from method)
(3) If all chars match i.e., processing goes till the end of the method and returns true
public boolean allSameLetter(String str) {
char c1 = str.charAt(0);
for(int i=1;i<str.length;i++) {
char temp = str.charAt(i);
if(c1 != temp) {
//if chars does NOT match,
//just return false from here itself,
//there is no need to verify other chars
return false;
}
}
//As it did NOT return from above if (inside for)
//it means, all chars matched, so return true
return true;
}
As Andrew said, you are decreasing i within your for loop. You can fix this by changing it to int charb4 = i - 1;. As for making your code more efficient you could condense it down to this.
public boolean allSameLetter(String str) {
for(char c : str.toCharArray())
if(c != str.charAt(0)) return false;
return true;
}
Comment if you don't understand a part of it :)
public boolean allSameLetter(String str)
{
for (int i = 1; i < str.length() -1; i++)
{
if ( str.charAt(i) != str.charAt(i+1))
{
return false;
}
}
return true
}
-1 is there since I am checking the current value in the array, then the next value in the array, thus I need to stop a place earlier.
If the loop if statement is never entered, it will make it far enough into the code to return true
You have to create a for loop that searches through the length of the String - 1. this way the program will not crash because of a 3 letter word with the program trying to get the 4th letter. This is what works for me:
public boolean allSameLetter(String str)
{
for(int i = 0; i< str.length()-1; i++){
if (str.charAt(i) != str.charAt(i+1)){
return false;
}
}
return true;
}
if((new HashSet<Character>(Arrays.asList(s.toCharArray()))).size()==1)
return true;
return false;
This should be enough
The bug is caused by
int charb4 = i--;
this line is equal to
int charb4 = i-1;
i=i-1;
Because of this, your loop will never stop.
The easiest way to fix this
public boolean allSameLetter(String str)
{
for (int i = 1; i < str.length(); i++)
{
if ( str.charAt(i) != str.charAt(i-1))
{
return false;
}
}
}
Method should take in a word, recursively go thru the string and find letters that are the same distance from either end of the alphabet and remove them. If it removes a match, those letters cannot be used again. If every letter is removed, then it is a match.
for (int i = 1; i < word.length()-1; i++)
{
if (word.charAt(0) + word.charAt(i) == 155)
{
StringBuilder sb = new StringBuilder(word);
sb.deleteCharAt(0);
sb.deleteCharAt(i);
String strNew = sb.toString();
System.out.println(strNew);
return isAlphaOpp(strNew);
}
}
return false;
}
I have modified your method a bit, have a look at it. You need compare with 155 if your string is all capitals, if all lower case letters you need compare with 219. As #Raghu suggested, this doesnt required recursion (that is making things complicated), but I am assuming you want to try this with recursion.
public static boolean isAlphaOpp (String word)
{
//if word has odd number of characters, it cannot be an alpha opp
if (word.length() % 2 != 0)
{
return false;
}
//if string makes it to 0, then word must be an alpha opp
if (word.length() == 0)
{
return true;
}
/*if (word.charAt(0) + word.charAt(word.length()-1) == 155)
{
System.out.println(word.substring(1, word.length()-1));
return isAlphaOpp(word.substring(1, word.length()-1));
}
*/
//Should go thru each letter and compare the values with char(0). If char(0) + //char(i) == 155 (a match) then it should remove them and call the method again.
int length = word.length()-1;
int start = 0;
String newStr = null;
while(start < length) {
if(word.charAt(start) + word.charAt(length) == 219) {
StringBuilder sb = new StringBuilder(word);
sb.deleteCharAt(length);
sb.deleteCharAt(start);
newStr = sb.toString();
System.out.println(newStr);
start++;
length--;
break;
} else {
start++;
}
}
if(newStr != null) {
return isAlphaOpp(newStr);
}
return false;
}
Here is the problem: Return true if the string "cat" and "dog" appear the same number of times in the given string. Examples: catDog("catdog") → true; catDog("catcat") → false; catDog("1cat1cadodog") → true
public boolean catDog(String str) {
int countCat=0;
int countDog=0;
for (int i=0; i<str.length();i++)
{
if (str.charAt(i)== 'c'&& str.length()>=3)
{
if (str.substring(i,i+3).equals("cat"))
countCat++;
}
}
for (int i=0; i<str.length();i++)
{
if (str.charAt(i)== 'd' && str.length()>=3)
{
if (str.substring(i,i+3).equals("dog"))
countDog++;
}
}
if (countCat == countDog)
return true;
else
return false;
}
In your for loops conditions you are checking if entire String has length greater or equal 3 instead of checking only part from i till end. Try maybe with
str.length() - i >= 3
instead of
str.length() >= 3
str.substring(i,i+3).equals("cat")
i might be the last and i+3 will give an error
Why don't you simply use StringUtils#countMatches?
StringUtils.countMatches(myStr, "cat") == StringUtils.countMatches(myStr, "dog");
Don't get lost with the indexes. However, if you don't want to use this method, debugging your code is the best thing you can do.
Okay, this is what I might do:
The problem was with your check str.length() >= 3. It should have been i + str.length().
I also suggest some changes to your code to get rid of duplication. Here I extracted the part that counts the number of appearances of a substring and moved it to its own method. The part that checks if count of cat equals count of dog now calls said method twice.
public static void main(String[] args) {
System.out.println(catDog("catdog"));
System.out.println(catDog("catcat"));
System.out.println(catDog("1cat1cadodog"));
System.out.println(catDog("catdogcatc"));//Would previously throw error.
}
public static boolean catDog(String str) {
int countCat = countAppearances(str, "cat");
int countDog = countAppearances(str, "dog");
return countCat == countDog;
}
private static int countAppearances(String str, String key) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == key.charAt(0) && i + key.length() <= str.length()) {
if (str.substring(i, i + key.length()).equals(key)) {
count++;
}
}
}
return count;
}
You need to update your first condition before spiting you string like:
if (str.charAt(i)== 'c' && (str.length() - i) >= 3)
{
if (str.substring(i,i+3).equals("cat"))
countCat++;
}
public boolean catDog(String str) {
int catCount = 0, dogCount = 0;
//run a for loop to check cat count
//run loop till 2nd last character
for (int i = 0; i < str.length() - 2; i++) {
//now check if the charaters at positions matches "cat"
//if matches then increment cat count
if (str.charAt(i) == 'c' && str.charAt(i + 1) == 'a' && str.charAt(i + 2) == 't') {
catCount++;
} else if (str.charAt(i) == 'd' && str.charAt(i + 1) == 'o' && str.charAt(i + 2) == 'g') {
//else check if the charaters at positions matches "dog"
//if matches then increment dog count
dogCount++;
}
}
//check cat count and dog count
if (catCount == dogCount) {
return true;
} else {
return false;
}
}
I am attempting to write a method that tests to see if a string is a palindrome. This is what I have so far:
public static boolean isPalindrome(String word) {
boolean flag = false;
if (word.length() < 2) {
flag = true;
} else if (word.charAt(0) == word.charAt(word.length() - 1)) {
flag = isPalindrome(word.substring(1, word.length() - 2));
}
return flag;
}
The problem I'm running into is, this method consistently returns true for strings of the form "aaaba", where the pair that should cause false to be propagated back through the stack is in the middle of the string. I'm banging my head against the wall trying to see where my error is, but to no avail. Perhaps a fresh set of eyes will see something I'm missing?
In your recursive call, you should subtract 1 from the length of the string, not 2:
// start index inclusive, end index exclusive
flag = isPalindrome(word.substring(1, word.length() - 1));
Change the endIndex in the substring(startIndex, endIndex) method. Note that according to Java Docs:
public String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1.
Change it to:
word.substring(1, word.length() - 1)
so, let's say word = "aaba", this method will return "ab".
Also, you can simplify your code by getting rid of flag and returning the result directly:
public static boolean isPalindrome(String word)
{
if (word.length() < 2) {
return true;
} else if (word.charAt(0) == word.charAt(word.length() - 1)) {
return isPalindrome(word.substring(1, word.length() - 1));
} else {
return false;
}
}
Try this....
public static boolean isPalindrome(String word) {
if(word.length() == 0 || word.length() == 1)
return true; // If length is 0 or 1 then it is palindrome
if(word.charAt(0) == word.charAt(word.length()-1))
return isPalindrome(word.substring(1, word.length()-1));
//Check the first and last char of the string if they are same
//then continue the same for a substring by removing the first
//and last character. And continue this until the string completes
return false; //I fthe case doesn't match
}