I need to find the index of the first distinct character between two strings using a recursive method.
Examples with expected outputs:
rFirstDistinctPlace("Little parcels", "Little pretzels") -> 8
rFirstDistinctPlace("Gold shadow", "gold shadow") -> 0
rFirstDistinctPlace("gold", "golda") -> 4
rFirstDistinctPlace("gold","gold") -> -1
Note:
I can't use the .equals() function
The thing I'm struggling with is that I need to return -1 if the strings are equal, otherwise it works fine.
Here's my code:
public static int rFirstDistinctPlace (String s1, String s2) {
if (smallestString(s1,s2).length()==0){
return 0;
}
if(s1.charAt(0)!=s2.charAt(0))
return rFirstDistinctPlace(s1.substring(0,0),s2.substring(0,0));
return 1+rFirstDistinctPlace(s1.substring(1),s2.substring(1));
}
This is the helper method smallestString:
public static String smallestString (String s1, String s2){
if(s1.length()>s2.length()){
return s2;
}
else if (s2.length()>s1.length()){
return s1;
}
else
return s1;
}
Thank you!
Recursive Solution:
If the two strings are empty, this means they are equal, return -1
Else if one of them is empty or the first characters don't match, return 0
Else recur with the substrings, if the result is -1, return it, else return it plus 1
public static void main(String[] args) {
System.out.println(rFirstDistinctPlace("Little parcels", "Little pretzels")); //8
System.out.println(rFirstDistinctPlace("Gold shadow", "gold shadow")); //0
System.out.println(rFirstDistinctPlace("gold", "golda")); //4
System.out.println(rFirstDistinctPlace("gold","gold")); //-1
}
public static int rFirstDistinctPlace (String s1, String s2) {
if(s1.isEmpty() && s2.isEmpty()) return -1;
else if (s1.isEmpty() || s2.isEmpty() || s1.charAt(0) != s2.charAt(0)) return 0;
int index = rFirstDistinctPlace(s1.substring(1), s2.substring(1));
return index == -1 ? index : 1 + index;
}
Iterative Solution:
Iterate over the two strings using a for-loop until it reaches the end of one of them
If the characters of the two strings at the current index are different, return i
At the end, if the two strings have different lengths, return i, else return -1
public static int rFirstDistinctPlace (String s1, String s2) {
int i = 0;
for(i = 0; i < s1.length() && i < s2.length(); i++) {
if(s1.charAt(i) != s2.charAt(i)) {
return i;
}
}
return s1.length() != s2.length() ? i : -1;
}
It's not so complex.
Return -1 if the strings are equal.
Return 0 if the length of one of the strings is 0 or if their first characters do not match.
Otherwise, return 1 + rFirstDistinctPlace(s1.substring(1), s2.substring(1)).
Demo:
class Main {
public static void main(String[] args) {
System.out.println(rFirstDistinctPlace("Little parcels", "Little pretzels")); // 8
System.out.println(rFirstDistinctPlace("Gold shadow", "gold shadow"));// 0
System.out.println(rFirstDistinctPlace("gold", "golda"));// 4
System.out.println(rFirstDistinctPlace("gold", "gold"));// -1
}
public static int rFirstDistinctPlace(String s1, String s2) {
if (Objects.equals(s1, s2))
return -1;
if (s1.length() == 0 || s2.length() == 0 || s1.charAt(0) != s2.charAt(0))
return 0;
return 1 + rFirstDistinctPlace(s1.substring(1), s2.substring(1));
}
}
Output:
8
0
4
-1
Related
I needed to Write a recursive method to compare two Strings using alphabetical order without using compareTo.
string1 comes before string2 returns an integer less than 0
string1 == (or indistinguishable from) string2 returns 0
string1 comes after string2 returns an integer greater than 0
I have written a method that works just fine, the problem is that if I compare two similar string or a string to itself it returns 1 instead of 0.
Any idea how can I optimize my method so it is not too long and does not fail to compare two identical strings?
I think part of my problem is because I declared my variable static, but not sure how I should work it out to declare them inside the method.
Code:
public class test{
public static String s1 = "alpha";
public static String s2 = "delta";
public static String s3 = "omega";
public static String s4 = "alpha";
public static int result;
public static void main (String[]args){
System.out.println(recursiveCompare(s1,s2)); // -1 good
System.out.println(recursiveCompare(s3,s1)); // 1 good
System.out.println(recursiveCompare(s4,s1)); // 1 FAIL!!! should be 0
System.out.println(recursiveCompare(s2,s3)); // -1 good
System.out.println(recursiveCompare(s1,s1)); // -1 FAIL!!! should be 0
}
public static int recursiveCompare(String s1, String S2){
if (s1.length() ==0 || s2.length()==0){
if ((s1.length() ==0 && s2.length()==0)){result = 0;}
else if ((s1.length() !=0 || s2.length()==0)){result = 1;}
else if ((s1.length() ==0 || s2.length()!=0)){result = -1;}
}
else
{
recursiveCompareHelper(s1, s2,0);
}
return result;
}
public static int recursiveCompareHelper(String s1,String s2, int index){
try{
if (s1.regionMatches(true,index,s2,index,1)){
result = recursiveCompareHelper(s1,s2,(index+1));}
else {
if (s1.charAt(index) > s2.charAt(index)){
result =1;
}
else if (s1.charAt(index) < s2.charAt(index)){
result =-1;
}
else if (s1.charAt(index) == s2.charAt(index)){
result = recursiveCompareHelper(s1,s2,(index+1));
}
}
} catch (StringIndexOutOfBoundsException e){
if (s1.charAt(index)==0 && s2.charAt(index)== 0){result = 0;}
else if (s1.charAt(index)==0 && s2.charAt(index)!= 0){result = 1;}
else if (s1.charAt(index)!=0 && s2.charAt(index)== 0){result =-1;}
}
return result;
}
}
first of all, notice you pass S2 as a parameter to recursiveCompare, not s2,
so actually you compare everything with "delta" because s2 is a static variable.
second of all, when comparing strings, as soon as you find a difference you can return an answer, its wrong just to change the value of result because it can be changed again later and return a wrong answer.
this is my solution, inside each recursive call I compare between the first letters and
if they're equal, I call the function recursively without the first letters of the strings
public class test {
public static String s1 = "alpha";
public static String s2 = "delta";
public static String s3 = "omega";
public static String s4 = "alpha";
public static void main(String[] args) {
System.out.println(recursiveCompare(s1, s2)); // -1 good
System.out.println(recursiveCompare(s3, s1)); // 1 good
System.out.println(recursiveCompare(s4, s1)); // 1 FAIL!!! should be 0
System.out.println(recursiveCompare(s2, s3)); // -1 good
System.out.println(recursiveCompare(s1, s1)); // -1 FAIL!!! should be 0
}
public static int recursiveCompare(String s1, String s2) {
if (s1.length() == 0 || s2.length() == 0) {
if ((s1.length() == 0 && s2.length() == 0)) {
return 0;
} else if (s1.length() != 0) {
return 1;
} else {
return -1;
}
}
if (s1.charAt(0) < s2.charAt(0)) {
return -1;
} else if (s1.charAt(0) > s2.charAt(0)) {
return 1;
} else if (s1.charAt(0) == s2.charAt(0)) {
return 0;
} else {
return recursiveCompare(s1.substring(1), s2.substring(1));
}
}
}
output:
-1
1
0
-1
0
You do not need to use the .langth() method. To compare strings, you need to use .equals()
public static int recursiveCompare(String s1, String s2){
if (s1.equals(s2)) {
return 0;
}
else
{
recursiveCompareHelper(s1, s2,0);
}
return result;
}
And in recursiveCompare(String s1, String S2) you hava S2 insted of s2.
Main mistake that you have done in your program is in function recursiveCompare you have taken argument as S2 and in a function using variable s2 which is declared as static variable so your function is failing to give correct result. Remember java is case sensitive language and in that case S2 is not same as s2.
below is the program which i have modified use that for your understanding.
public class Test{
/* public static String s1 = "alpha";
public static String s2 = "delta";
public static String s3 = "omega";
public static String s4 = "alpha";*/
public static int result;
public static void main (String[]args){
String s1 = "alpha";
String s2 = "delta";
String s3 = "omega";
String s4 = "alpha";
System.out.println(recursiveCompare(s1,s2)); // -1 good
System.out.println(recursiveCompare(s3,s1)); // 1 good
System.out.println(recursiveCompare(s4,s1)); // 1 FAIL!!! should be 0
System.out.println(recursiveCompare(s2,s3)); // -1 good
System.out.println(recursiveCompare(s1,s1)); // -1 FAIL!!! should be 0
}
public static int recursiveCompare(String s1, String S2){
if (s1.length() ==0 || S2.length()==0){ // here you have to use S2 and not s1
if ((s1.length() ==0 && S2.length()==0)){result = 0;}
else if ((s1.length() !=0 || S2.length()==0)){result = 1;}
else if ((s1.length() ==0 || S2.length()!=0)){result = -1;}
}
else
{
recursiveCompareHelper(s1, S2,0);
}
return result;
}
public static int recursiveCompareHelper(String s1,String s2, int index){
// System.out.println("String are" + s1+" "+ s2 + " index is "+ index);
if(index<s1.length()) {
// System.out.println("Characters at index : "+ s1.charAt(index)+ " "+ s2.charAt(index));
if (s1.charAt(index) > s2.charAt(index)){
//System.out.println("In the if condition");
result= 1;
}
else if (s1.charAt(index) < s2.charAt(index)){
//System.out.println("In the else if condition");
result =-1;
}
else if (s1.charAt(index) == s2.charAt(index)){
//System.out.println("Character at "+index);
result = recursiveCompareHelper(s1,s2,(index+1));
}
}
else return 0;
return result;
}
}
I started learning Java, currently I'm playing around with recursion.
I wanted to try and make a substring method which will substring from both sides by 1 character until we get the desired string.
I managed to do the first part but I'm having problem figuring out how to substring from the back.
n and m should be the indexes between which we want to substring (inclusive).
In this example result of method should be "bstri"
Here is my code:
public static void main(String[] args) {
String s = "substringme";
System.out.println(rec(s,2,6));
}
public static String rec(String s, int n, int m) {
if(n == 0 /* && missing 2nd part of condition */){
return s;
} else {
if(n>0){
s = s.substring(1);
n--;
}
if(/* missing condition */){
s= s.substring(0, s.length()-1);
}
return rec(s,n,m);
}
}
I would appreciate any help I can get.
So fixing your recursive method is fairly easy. We just do exactly the same as you did for n:
public static String rec(String s, int n, int m) {
if (n == 0 && m == 0) {
return s;
}
else {
if(n > 0) {
s = s.substring(1);
n--;
}
if(m > 0) {
s = s.substring(0, s.length()-1);
m--;
}
return rec(s,n,m);
}
}
The issue now is that the value of m given as input is measured from the start of the String and it would be way more convenient for us if it were measured from the end of the String.
We can introduce a new method to do this for us which acts as our entry point to the recursive method:
public static String substr(String s, int n, int m) {
final int newM = s.length() - m - 1; //-1 to be inclusive of the char
return rec(s, n, newM);
}
You would then change your main method to call substr() instead:
public static void main(String[] args) {
String s = "substringme";
System.out.println(substr(s,2,6));
}
I often find myself writing these kind of "entry point" methods when I'm using recursive methods. If you were doing this properly, substr would be your public-facing method and rec would be private.
I propose that you do m-- in the first loop because the definition of m as an index changes when you shorten s from the front.
public static String rec(String s, int n, int m) {
if(n == 0 && m == s.length() - 1){
return s;
} else {
if (n > 0) {
s = s.substring(1);
n--;
m--;
}
if (m < s.length() - 1) {
s = s.substring(0, s.length() - 1);
}
return rec(s, n, m);
}
}
What you could do is look for the difference between m and the length of the string, and cut off characters from the end of the string until it is the correct length.
import java.util.*;
public class Test {
public static void main(String[] args) {
String s = "substringme";
System.out.println(rec(s,2,6));
}
public static String rec(String s, int n, int m) {
if(n == 0 && s.length()-m < 1){
return s;
} else {
if(s.length()-m > 1){
s= s.substring(0, s.length()-1);
}
else if(n>0){
s = s.substring(1);
n--;
}
return rec(s,n,m);
}
}
}
Also shouldn't substring from 2 to 6 be "bstr", not "bstri"?
System.out.println(s.substring(2, 6)); //equals bstr
I'm trying to code this one up,but I don't get an expected result:
Given a string, compute recursively (no loops) the number of lowercase 'x' chars in the string.
countX("xxhixx") → 4
countX("xhixhix") → 3
countX("hi") → 0
Here is my method:
public int countX(String str) {
int count = 0;
if(str.length() >= 1 ) {
if(str.substring(0, 1).equals("x")) {
str = str.substring(1, str.length());
count = count + 1 + countX(str);
}
}
else {
str = str.substring(1, str.length());
count = count + countX(str);
}
return count;
}
You had the right idea, but I think you over complicated things. Just check explicitly if the first character is x (as you have), and only increment count in that case. Regardless of whether it was or wasn't, continue recursing on:
public static int countX(String str) {
int count = 0;
if (str.length() > 0) {
if (str.substring(0, 1).equals("x")) {
++count;
}
str = str.substring(1, str.length());
count += countX(str);
}
return count;
}
Suppose you have a string "axbxcx". The code below looks only at the first character in the string and determines if it is an x. If so, then return 1 in addition to the number of x's found in the rest of the string. If the first character is not an x, then the number of x's in the string is equal to the number of x's in the string not including the first character, so that is what is returned.
int count(String s)
{
if (s.length() == 0) // base case
{
return 0;
}
if (s.charAt(0) == 'x')
{
return 1 + count(s.substring(1));
}
else
{
return count(s.substring(1));
}
}
How about this?
public static int countX(String str) {
if (str.length() == 0) {
return 0;
}
if (str.substring(0, 1).equals("x")) {
return 1 + countX(str.substring(1));
}
return countX(str.substring(1));
}
You should try this (it assumes you are testing outside the method that initial str value is not null and has a length greater than 0).
public int countX(String str) {
if ( str.length() == 1 ) {
return ("x".equalsTo(str) ? 1 : 0);
} else {
return (str.charAt(0) =='x' ? 1 : 0) + countX(str.substring(1,str.length())
}
}
Here is a simple way to do it.
First, check if the string is empty. This is the terminating condition of the recursion.
Then your result is simply the count for the first character (1 or 0), added to the count for the rest of the string (calculated by calling your function on substring(1)).
public static int countX(String str) {
if (str.isEmpty()) {
return 0;
}
return (str.charAt(0)=='x' ? 1 : 0) + countX(str.substring(1));
}
you can try this one:
public int countX(String str) {
int end = str.length(); //get length of the string
int counter = 0;
if(str.length()==0){
return counter; //recursion will stop here
}else{
if(str.charAt(end-1) == 'x'){
counter++;
}
end--;
str=str.substring(0,end); //your string will perform a decrease in length and the last char will be removed
}
return counter+countX(str);
}
public int indexOf(String s1,String s2){
if(s1.length()<s2.length())
return -1;
else if(s1.substring(s1.length()-s2.length()).equals(s2))
return s1.length()-s2.length();
else
return indexOf(s1.substring(0,s1.length()-1),s2);
}
I wrote this method to get index of the second string in the first one
but it has a bug it cant effectively return first occurrence of the second string this is because I am using logic to find the second string from backwards and I could not think of any other logic. Any suggestions would be appreciated.
example of a failure case: firstString "BarackObama" second string "a"
As you noted, you are doing it backwards. Instead, you should go forward:
public static int indexOf(String s1, String s2){
if(s1.length()<s2.length()) {
return -1;
}
else if(s1.substring(0, s2.length()).equals(s2)) {
return 0;
}
else {
int i = indexOf(s1.substring(1, s1.length()), s2);
if (i == -1) {
return i;
} else {
return 1 + i;
}
}
}
Example:
String s1 = "BarackObama";
String s2 = "rac";
indexOf(s1, s2);
It would run like this:
indexOf("BarackObama", "rac"):
"BarackObama".substring(0, 3).equals("rac") -> false
return 1 + indexOf("BarackObama".substring(1, 11), "rac")
indexOf("arackObama", "rac"):
"arackObama".substring(0, 3).equals("rac") -> false
return 1 + indexOf("arackObama".substring(1, 10), "rac")
indexOf("rackObama", "rac"):
"rackObama".substring(0, 3).equals("rac") -> true
return 0;
return 0 + 1 + 1 = 2
I think the way you are using subString(int startIndex), it would give you characters from the end. If you pass only on parameter to the subString method, it acts as the start index for the subString it returns. So in case your example, you would get the "a" at the end of "BarackObama". You can try to use substring(int beginIndex, int endIndex).
Here it is.
private int indexOf(String s1,String s2){
int index=0;
if( s1 ==null || s2 ==null){
return -1;
}else if(s1.length()<s2.length()){
return -1;
}else{
index=s1.lastIndexOf(s2);
s1=s1.substring(0,index);
if(!s1.contains(s2)){
return index;
}else{
return indexOf(s1,s2);
}
}
}
public int indexOf(String s1, String s2){
if (s1.length() < s2.length()){
return -1;
} else {
int n = 0;
int index = findIndex(s1, s2, n);
return index;
}
}
private int findIndex(String s1, String s2, int n){
if (s1.substring(n, (n + s2.length())).equals(s2)){
return n;
} else if ((n < s1.length() - s2.length())){
return findIndex(s1, s2, (n + 1));
} else{
return -1;
}
}
I need to write a recursive method called indexOf that accepts two Strings as parameters and that returns the starting index of the first occurrence of the second String inside the first String (or -1 if not found). I have to solve this problem using recursion.
These are some example results:
indexOf("Barack Obama", "Bar") 0
indexOf("Barack Obama", "ck") 4
indexOf("Barack Obama", "a") 1
indexOf("Barack Obama", "McCain") -1
indexOf("Barack Obama", "BAR") -1
This is my solution but it gives me 6 for indexOf("Barack Obama", "McCain")instead of -1.
public static int indexOf(String s1, String s2) {
if(s1.equals(s2))
return 0;
else
return indexOfHelper(s1, s2, 0);
}
private static int indexOfHelper(String s1, String s2, int ctr) {
if(s2.length() > s1.length())
return -1;
if(s2.length() == 0 || s1.length() == 0) //base case
return ctr;
else //recursive case
if(s1.charAt(0) == s2.charAt(0)){ //if there is a matching character
if(s1.substring(0, s2.length()).equals(s2))
return ctr; //if there is a matching character and the rest of the strings match as well
else
return -1; //if there is a matching character but the rest of the strings don't match
}
else
return 1 + indexOfHelper(s1.substring(1), s2, ctr);
}
I must say that recursive step was quite tricky. It passes all test cases. I hope following helps. Java and C++ code here
JAVA
int indexOf (String s1, String s2) {
if (s1.length() == 0 && s2.length() == 0) {
return 0;
} else if (s1.length() == 0) {
return -1;
} else if (s2.length() == 0) {
return 0;
} else if (s2.length()>s1.length()) {
return -1;
}
else if (s1.charAt(0) == s2.charAt(0)) {
int subIndex = indexOf(s1.substring(1),s2.substring(1));
return subIndex == -1 ? -1: subIndex;
} else {
int subIndex = indexOf(s1.substring(1),s2);
return subIndex == -1 ? -1: subIndex+1;
}
}
C++ code here
int indexOf (string s1, string s2) {
if (s1 == "" && s2 == "") {
return 0;
} else if (s1 == "") {
return -1;
} else if (s2=="") {
return 0;
} else if (s2.size()>s1.size()) {
return -1;
}
else if (s1 [0] == s2[0]) {
int subIndex = indexOf(s1.substr(1,s1.length()),s2.substr(1,s2.length()));
return subIndex == -1 ? -1: subIndex;
} else {
int subIndex = indexOf(s1.substr(1,s1.length()),s2);
return subIndex == -1 ? -1: subIndex+1;
}
}
Here is my solution
public static void main(String[] args) {
System.out.println(indexOf("Ninja!", "i"));
System.out.println(indexOf("ninja2", "ja2"));
System.out.println(indexOf("ninja2", "hae"));
}
public static int indexOf(String s, String contains) {
if (contains.length() > s.length()) {
return -1;
}
return indexOf(s, contains, 0);
}
private static int indexOf(String s, String contains, int index) {
if ((s.length() - contains.length()) < index) {
return -1;
}
if (s.substring(index, index + contains.length()).equals(contains)) {
return index;
} else {
return indexOf(s, contains, index + 1);
}
}