I am trying to find the first occurrence of a letter in a string. For example, p in apple should return 1. Here is what I have:
// Returns the index of the of the character ch
public static int indexOf(char ch, String str) {
if (str == null || str.equals("")) {
return -1;
} else if(ch == str.charAt(0)) {
return 1+ indexOf(ch, str.substring(1));
}
return indexOf(ch, str.substring(1));
}
It just doesn't seem to be returning the correct value.
I'll give you some hints:
When you've found the letter, you don't need to recurse further. Additionally, think about what you should be returning in this case.
When do you recurse, also think about what the function should return.
Is there anything special you need to do if the recursive call returns -1?
Your attempt was good, but not quite there. Here is a correct implementation based off yours:
public static int indexOf(char ch, String str) {
// Returns the index of the of the character ch
if (str == null || str.equals("")) {
// base case: no more string to search; return -1
return -1;
} else if (ch == str.charAt(0)) {
// base case: ch is at the beginning of str; return 0
return 0;
}
// recursive step
int subIndex = indexOf(ch, str.substring(1));
return subIndex == -1 ? -1 : 1 + subIndex;
}
There were two problems with your attempt:
In the else if part, you had found the character, so the right thing to do was stop the recursion, but you were continuing it.
In your last return statement, you needed to be adding 1 to the recursive call (if the character was eventually found), as a way of accumulating the total index number.
Here's another variation. Instead of calling substring you could modify the function a bit to pass the next index to check. Notice that the recursion is initiated with index 0. (You could actually start on any index. There is also some error checking in case the letter isn't found. Looking for x in apple will return -1.)
public static void main(String []args){
System.out.println("Index: " + indexOf('e', "apple", 0));
System.out.println("Index: " + indexOf('x', "apple", 0));
System.out.println("Index: " + indexOf('p', "Mississippi", 3));
System.out.println("Index: " + indexOf('p', "Mississippi", 908));
}
public static int indexOf(char ch, String str, int idx) {
// check for garbage data and incorrect indices
if (str == null || str.equals("") || idx > str.length()-1)
return -1;
// check to see if we meet our condition
if (ch == str.charAt(idx))
return idx;
// we don't match so we recurse to check the next character
return indexOf(ch, str, idx+1);
}
Output:
Index: 4
Index: -1
Index: 8
Index: -1
Well if we must use recursion then try this:
class RecursiveFirstIndexOf {
public static void main(String[] args) {
System.out.println(indexOf('p', "apple", 0));
}
static int indexOf(char c, String str, int currentIdx) {
if (str == null || str.trim().isEmpty()) {
return -1;
}
return str.charAt(0) == c ? currentIdx : indexOf(c, str.substring(1), ++currentIdx);
}}
Why not doing it straight forward?
public static void main(String[] args) {
String str = "abcdef";
for (int idx = 0; idx < str.length(); idx++) {
System.out.printf("Expected %d, found %d\n", idx, indexOf(str.charAt(idx), str, 0));
}
System.out.printf("Expected -1, found %d\n", indexOf(str.charAt(0), null, 0));
}
public static int indexOf(char ch, String str, int index) {
if (str == null || index >= str.length()) return -1;
return str.charAt(index) == ch ? index : indexOf(ch, str, ++index);
}
OUTPUT:
Expected 0, found 0
Expected 1, found 1
Expected 2, found 2
Expected 3, found 3
Expected 4, found 4
Expected 5, found 5
Expected -1, found -1
first of all : Recursion has two pillars, Base Case and General Case.
Base Case (the termination point) is the one where Recursion terminates and General Case as the name implies is where the program keeps executing until it finds Base Case
you may try this example, where count is a global static variable
public static int indexOf(char ch, String str)
{
// Returns the index of the of the character ch
if (str == null || str.Equals("")) //Base Case
{
if (count != 0)
{
if(str.Length == 0)
return -1;
return count;
}
else
return -1;
}
else if (ch == str.CharAt(0)) //Base Case
return 1 + count;
count++;
return indexOf(ch, str.Substring(1)); //General Case
}
Related
I recently got an interview question where I needed to implement regular expression matching in Java without using regex matching.
Given an input string (s) and a pattern (p), implement regular
expression matching with support for '.', '+', '*' and '?'.
// (pattern, match) --> bool does it match
// operators:
// . --> any char
// + --> 1 or more of prev char
// * --> 0 or more of prev char
// ? --> 0 or 1 of prev char
// (abc+c, abcccc) --> True
// (abcd*, abc) --> True
// (abc?c, abc) --> True
// (abc.*, abcsdfsf) --> True
I came up with below code which only implements '.' and '*' but not able to figure out how to implement others:
public static boolean isMatch(String s, String p) {
if (p.length() == 0) {
return s.length() == 0;
}
if (p.length() > 1 && p.charAt(1) == '*') { // second char is '*'
if (isMatch(s, p.substring(2))) {
return true;
}
if (s.length() > 0 && (p.charAt(0) == '.' || s.charAt(0) == p.charAt(0))) {
return isMatch(s.substring(1), p);
}
return false;
} else { // second char is not '*'
if (s.length() > 0 && (p.charAt(0) == '.' || s.charAt(0) == p.charAt(0))) {
return isMatch(s.substring(1), p.substring(1));
}
return false;
}
}
Also what is the best way to implement this problem?
Here is untested code. The idea is that we keep track of where we are in the string and the pattern. This would NOT be a good approach to try to extend to a full RE engine (just consider what adding parentheses would take), but is fine for this case:
public static boolean isMatch (String p, String s, int pos_p, int pos_s) {
if (pos_p == p.length()) {
// We matched the whole pattern.
return true;
}
else if (pos_s == s.length()) {
// We reached the end of the string without matching.
return false;
}
else if (pos_p == -1) {
// Do we match the pattern starting next position?
if (isMatch(p, s, pos_p + 1, pos_s + 1)) {
return true;
}
else {
// Try to match the pattern starting later.
return isMatch(p, s, pos_p, pos_s + 1);
}
}
else {
char thisCharP = p.charAt(pos_p);
char nextCharP = pos_p + 1 < p.length() ? p.charAt(pos_p + 1) : 'x';
// Does this character match at this position?
boolean thisMatch = (thisCharP == s.charAt(pos_s));
if (thisCharP == '.') {
thisMatch = true;
}
if (nextCharP == '*') {
// Try matching no times - we don't need thisMatch to be true!
if (isMatch(p, s, pos_p + 2, pos_s)) {
return true;
}
else {
// Try matching 1+ times, now thisMatch is required.
return thisMatch && isMatch(p, s, pos_p, pos_s + 1);
}
}
else if (nextCharP == '+') {
if (! thisMatch) {
// to match 1+, we have to match here.
return false;
}
else if (isMatch(p, s, pos_p + 2, pos_s + 1)) {
// We matched once.
return true;
}
else {
// Can we match 2+?
return isMatch(p, s, pos_p, pos_s + 1);
}
}
else if (thisMatch) {
// Can we match the rest of the pattern?
return isMatch(p, s, pos_p + 1, pos_s + 1);
}
else {
// We didn't match here, this is a fail.
return false;
}
}
}
public static boolean isMatch (String p, String s) {
// Can we match starting anywhere?
return isMatch(p, s, -1, -1);
}
How do I remove a target character from string using RECURSION?
I know it begins like:
public static String removeChar(String str, char target) {
if (str.length() == 0) {
return str;
} else {
if (str.charAt(0) == target) {
return removeChar(/*what goes in here?*/)
}
return removeChar(/*what goes in here?*/)
}
}
thank you!!
The idea is that if the first character is equal to the target character, you simply return the result of removeChar() applied on the rest of the String (i.e. the String without the first character), which removes the first character.
On the other hand, if the first character is not equal to the target character, you return a String starting with the original first character and ending with the result of applying removeChar() on the rest of the String.
public static String removeChar(String str, char target) {
if(str.length() == 0) {
return str;
} else {
if(str.charAt(0) == target) {
// remote the first character, and apply the recursive method to
// the rest of the String
return removeChar(str.substring(1),target);
} else {
// don't remote the first character, and apply the recursive method to
// the rest of the String
return str.charAt(0) + removeChar(str.substring(1),target);
}
}
}
You could use the following code inside the else-block:
if(str.charAt(0) == target) {
return removeChar(str.substring(1), target);
}
return charAt(0) + removeChar(str.substring(1), target);
But I don't see a need to use recursion here, you could just use
str.replace(target, '');
You check the index of the 1st occurrence of the char and remove it from that position:
public static String removeChar(String str, char target) {
int index = str.indexOf(target);
if (index < 0)
return str;
else {
return removeChar(str.substring(0, index) + str.substring(index + 1), target);
}
}
public static void main(String[] args) {
String str = "0123045607890";
System.out.println(removeChar(str, '0'));
}
will print:
123456789
I suggest this solution.
function isLetter(str) {
return str.length === 1 && str.match(/[A-Z]/i);
}
function removeLetters(str) {
//console.log(str);
let val = str.substr(0, 1);
console.log(val);
if (isLetter(val)) {
return removeLetters(str.substr(1))
} else {
console.log("Return", str);
return str;
}
}
I have to write the following recursion method:
public static int countA(String s)
Yet I find it impossible to do this without declaring a counter and position variable; like so:
public static int countA(String s, int position) {
int count = 0;
if( s.charAt(position) == 'A' )
count++;
if( position + 1 < s.length() ) {
count += countA(s, position + 1);
}
return count;
}
How can I simplify my answer so that my method is the same as the one listed?
EDIT: Yes, I want to count all A's inside a string.
Try this:
public static int countA(String s) {
int count = 0;
if (s == null)
return 0;
if (s.length() == 0)
return 0;
if (s.charAt(0) == 'A')
count++;
return count + countA(s.substring(1));
}
There are two forms of recursion,
Tail Recursion : The return value is calculated as a combination of the value of current subroutine and the return value of the next call. Example,
int factorial(int a) {
if(a==0)
return 1;
else
return a * factorial( a-1 );
}
Accumulator based recursion : You accumulate the results by adding an additional parameter and return the accumulated value.
int factorial(int a, int fact) {
if(a==0)
return fact;
else
return factorial(a-1, a*fact);
}
Obviously what you have here is accumulator based, while you can improve it to Tail recursion.
Tail recursion is considered more readable, while it can cause a StackOverflow! (no pun intended). This is because it has to push the current value to a stack, before calling subroutine again. And when you make a large number of such calls, this stack might go over its limit.
Some compilers optimize tail recursion to accumulator based in order to avoid this problem.
What about:
public static int countA(String s) {
if(s==null) return 0;
if(s.length()==0) return 0;
if( s.charAt(0) == 'A' )
{
return 1 + countA(s.substring(1));
} else
{
return countA(s.substring(1));
}
}
I think something like this should do the trick
Here we are assuming that countA returns the number of As inside String s
public static int countA(String s)
{
if(s.length()==0) return 0; // return 0 if string is empty
else
{
// if the first char is A add 1 to the answer, recurse
if(s.toCharArray()[0])=='A') return 1+countA(s.subString(1,s.length()));
// else add nothing to the answer, recurse
else return countA(s.subString(1,s.length()));
}
}
You move the position variable out of the method and make it static(since your countA() is also static).
static int position = 0;
public static int countA(String s) {
int count = 0;
if( s.charAt(position) == 'A' )
count++;
if( position + 1 < s.length() ) {
position++;
count += countA(s);
}
return count;
}
Ideally you have the termination condition first, which then usually simplifies the code by reducing indentation/nesting and have a "do nothing" action for it (typically requiring one more iteration than absolutely required).
You also don't need the local variable!
public static int countA(String s, int position) {
if (position == s.length())
return 0;
return (s.charAt(position) == 'A' ? 1 : 0) + countA(s, position + 1);
}
I have a class that checks whether a string is a palindrome or not. I have two questions.
1) Is this the most efficient way to check for palindrome?
2) Can this be implemented recursively?
public class Words {
public static boolean isPalindrome(String word) {
String pal = null;
word = word.replace(" ", "");
pal = new StringBuffer(word).reverse().toString();
if (word.compareTo(pal) == 0) {
return true;
} else {
return false;
}
}
}
Have a test class to test this... Doubt its needed but here it is anyways if anyone cares to try it out to be able to help me with any of the two questions above...
public class testWords {
public static void main(String[] args) {
if (Words.isPalindrome("a") == true) {
System.out.println("true");
} else {
System.out.println("false");
}
if (Words.isPalindrome("cat") == true) {
System.out.println("true");
} else {
System.out.println("false");
}
if (Words.isPalindrome("w o w") == true) {
System.out.println("true");
} else {
System.out.println("false");
}
if (Words.isPalindrome(" a ") == true) {
System.out.println("true");
} else {
System.out.println("false");
}
if (Words.isPalindrome("mom!") == true) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
thanks in advance for any help and or input :)
To implement a 'palindrome check' recursively, you must compare if the first and last characters are the same. If they are not the same the string is most certainly not a palindrome. If they are the same the string might be a palindrome, you need to compare the 2nd character with the 2nd to last character, and so on until you have strictly less then 2 characters remaining to be checked in your string.
A recursive algorithm would look like this:
public static boolean isPalindrome(String word) {
//Strip out non-alphanumeric characters from string
String cleanWord = word.replaceAll("[^a-zA-Z0-9]","");
//Check for palindrome quality recursively
return checkPalindrome(cleanWord);
}
private static boolean checkPalindrome(String word) {
if(word.length() < 2) { return true; }
char first = word.charAt(0);
char last = word.charAt(word.length()-1);
if( first != last ) { return false; }
else { return checkPalindrome(word.substring(1,word.length()-1)); }
}
Note, that my recursion method is not most efficient approach, but
simple to understand
Marimuthu Madasamy has a more efficient recursive method, but is harder to understand
Joe F has listed an equivalently efficient iterative method
which is the best approach for implementation because it cannot cause a stack overflow error
Here is another recursive solution but using array which could give you some performance advantage over string in recursive calls (avoiding substring or charAt).
private static boolean isPalindrome(final char[] chars, final int from,
final int to) {
if (from > to) return true;
return chars[from] != chars[to] ? false
: isPalindrome(chars, from + 1, to - 1);
}
public static boolean isPalindrome(final String s) {
return isPalindrome(s.toCharArray(), 0, s.length() - 1);
}
The idea is that we keep track of two positions in the array, one at the beginning and another at the end and we keep moving the positions towards the center.
When the positions overlap and pass, we are done comparing all the characters and all the characters so far have matched; the string is palindrome.
At each pass, we compare the characters and if they don't match then the string is not palindrome otherwise move the positions closer.
It's actually sufficient to only check up to the middle character to confirm that it is a palindrome, which means you can simplify it down to something like this:
// Length of my string.
int length = myString.length();
// Loop over first half of string and match with opposite character.
for (int i = 0; i <= length / 2; i++) {
// If we find one that doesn't match then return false.
if (myString.charAt(i) != myString.charAt(length - 1 - i)) return false;
}
// They all match, so we have found a palindrome!
return true;
A recursive solution is very possible but it is not going to give you any performance benefit (and probably isn't as readable).
Can this be implemented Recursively?
YES
Here is example:
public static boolean palindrome(String str)
{
if (str.length()==1 || str.length == 0)
return true;
char c1 = str.charAt(0);
char c2 = str.charAt(str.length() - 1);
if (str.length() == 2)
{
if (c1 == c2)
return true;
else
return false;
}
if (c1 == c2)
return palindrome(str.substring(1,str.length() - 1));
else
return false;
}
My two cents. It's always nice to see the different ways people solve a problem. Of course this is not the most efficient algorithm memory or speed wise.
public static boolean isPalindrome(String s) {
if (s.length() <= 1) { // got to the middle, no need for more checks
return true;
}
char l = s.charAt(0); // first char
char r = s.charAt(s.length() - 1); // last char
if (l == r) { // same char? keep checking
String sub = s.substring(1, s.length() - 1);
return isPalindrome(sub);
}
return false;
}
The simplest way to check palindrome.
private static String palindromic(String word) {
if (word.length() <= 1) {
return "Polidramic";
}else if (word.charAt(0) != word.charAt(word.length() - 1)) {
return "Not Polidramic";
}
return palindromic(word.substring(1, word.length() - 1));
}
I have the following function.
private boolean codeContains(String name, String code) {
if (name == null || code == null) {
return false;
}
Pattern pattern = Pattern.compile("\\b" + Pattern.quote(name) + "\\b");
Matcher matcher = pattern.matcher(code);
return matcher.find();
}
It is called many thousand times in my code, and is the function in which my program spends the most amount of time in. Is there any way to make this function go faster, or is it already as fast as it can be?
If you don't need to check word boundaries, you might do this :
private boolean codeContains(String name, String code) {
return name != null && code != null && code.indexOf(name)>=0;
}
If you need to check word boundaries but, as I suppose is your case, you have a big code in which you often search, you could "compile" the code once by
splitting the code string using the split method
putting the tokens in a HashSet (checking if a token is in a hashset is reasonably fast).
Of course, if you have more than one code, it's easy to store them in a structure adapted to your program, for example in a map having as key the file name.
"Plain" string operations will (almost) always be faster than regex, especially when you can't pre-compile the pattern.
Something like this would be considerably faster (with large enough name and code strings), assuming Character.isLetterOrDigit(...) suits your needs:
private boolean codeContains(String name, String code) {
if (name == null || code == null || code.length() < name.length()) {
return false;
}
if (code.equals(name)) {
return true;
}
int index = code.indexOf(name);
int nameLength = name.length();
if (index < 0) {
return false;
}
if (index == 0) {
// found at the start
char after = code.charAt(index + nameLength);
return !Character.isLetterOrDigit(after);
}
else if (index + nameLength == code.length()) {
// found at the end
char before = code.charAt(index - 1);
return !Character.isLetterOrDigit(before);
}
else {
// somewhere inside
char before = code.charAt(index - 1);
char after = code.charAt(index + nameLength);
return !Character.isLetterOrDigit(after) && !Character.isLetterOrDigit(before);
}
}
And a small test succeeds:
#Test
public void testCodeContainsFaster() {
final String code = "FOO some MU code BAR";
org.junit.Assert.assertTrue(codeContains("FOO", code));
org.junit.Assert.assertTrue(codeContains("MU", code));
org.junit.Assert.assertTrue(codeContains("BAR", code));
org.junit.Assert.assertTrue(codeContains(code, code));
org.junit.Assert.assertFalse(codeContains("FO", code));
org.junit.Assert.assertFalse(codeContains("BA", code));
org.junit.Assert.assertFalse(codeContains(code + "!", code));
}
This code seemed to do it:
private boolean codeContains(String name, String code) {
if (name == null || code == null || name.length() == 0 || code.length() == 0) {
return false;
}
int nameLength = name.length();
int lastIndex = code.length() - nameLength;
if (lastIndex < 0) {
return false;
}
for (int curr = 0; curr < lastIndex; ) {
int index = code.indexOf(name, curr);
int indexEnd = index + nameLength;
if (index < 0 || lastIndex < index) {
break;
}
boolean leftOk = index == curr ||
index > curr && !Character.isAlphabetic(code.charAt(index - 1));
boolean rightOk = index == lastIndex ||
index < lastIndex && !Character.isAlphabetic(code.charAt(indexEnd));
if (leftOk && rightOk) {
return true;
}
curr += indexEnd;
}
return false;
}
The accepted answer goes to dystroy as he was the first to point me in the right direction, excellent answer by Bart Kiers though, +1!