-1

I'm trying to write a method that get two strings and checks if its able to substring them to get the same string. for example if s1 = "abc" and s2 = "abbbc" it will return true. I want to do it in BackTracking recursion.

here is my code that isn't working:

private static boolean isTrue(String s1, String s2) {
    boolean right= false;

    if(s1 == s2)
        right = true;

    else {
        if(s1.length() > 0 && s2.length() > 0) {
            right = isTrue(s1, s2.substring(1)) || isTrue(s1.substring(1), s2.substring(1));
        }
    }

    return right;
}

some help please?

Ohadyk
  • 29
  • 4
  • 1
    "isn't working" is not a precise enough error description for us to help you. *What* doesn't work? *How* doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? – Jörg W Mittag Jan 19 '19 at 18:46
  • 1
    Welcome to SO! What do you mean by "able to substring them to get the same string"? Please clarify with expected i/o and examples. – ggorlen Jan 19 '19 at 18:51
  • sorry, I mean this method returns "false" instead of "true". because s1 = "abc" and s2 = "abbbc", I can use s2.substring(3) to get s1. thats why I expect the method will return "true". – Ohadyk Jan 19 '19 at 18:56
  • While I am not sure what you are doing, an important thing about Java is that ```s1==s2``` does not compare the Strings, it compares the object references. You have to use ```s1.equals(s2)``` instead – tevemadar Jan 19 '19 at 19:05
  • "abc" is not a substring of "abbbc". – Tobb Jan 19 '19 at 19:10
  • s2.substring(3) is "bc", so it is not equal to s1. You need to explain what you are trying to achieve. – Tobb Jan 19 '19 at 19:18
  • I'll try to explain myself. I have two strings, for example s1 = " abcdef " and s2 = a * b c *. I want my method to check if I can change all the * with any chars and get s2 to be equal s1. in this example the method should return true, becouse I can change the first * with nothing and the second * with "def"... I hope its clear now – Ohadyk Jan 19 '19 at 19:20

1 Answers1

0

Assuming that you meant if the Second string had all the characters in the first string this would be the solution with recursion.

static boolean isTrue(String s1,String s2,int count) {
    if(count == s1.length()){
        return true;
    }
    else{
        CharSequence cs = new StringBuilder(s1.substring(count, count+1));
        if(s2.contains(cs)){
            s2 = s2.replaceFirst(s1.substring(count, count+1), "");
            return isTrue(s1,s2,count+1);
        }
        else{
            return false;
        }
    }
}

If you want to check if the first string is a subString of the second string you can do it without recursion with the contains method

Sanath Kumar
  • 163
  • 1
  • 12