How can i get the following result using only recursion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JennaCoder
    New Member
    • Mar 2013
    • 5

    How can i get the following result using only recursion

    Code:
    public static void main(String args[]) { 
     System.out.println(match("hello", "hello.")); // should return false
     System.out.println(match("hello", "jello")); // should return false
     System.out.println(match("hello", "h@llo")); // should return true
     System.out.println(match("hello", "h@@@@")); // should return true
     System.out.println(match("hello", "h*")); // should return true
     System.out.println(match("hello", "*l*")); // should return true
     System.out.println(match("anyString", "*")); // should return true
     System.out.println(match("help", "h@@@@")); // should return false
     System.out.println(match("help", "h*")); // should return true
     System.out.println(match("help", "*l*")); // should return true
     System.out.println(match("help", "*l*p")); // should return true
     System.out.println(match("help", "h@llo")); // should return false
     System.out.println(match("", "*")); // should return true
     System.out.println(match("", "***")); // should return true
     System.out.println(match("", "@")); // should return false
     }
    Last edited by Rabbit; Mar 31 '13, 07:00 AM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    I don't see what your code has to do with recursion.

    Comment

    • JennaCoder
      New Member
      • Mar 2013
      • 5

      #3
      This is what I did but it doesnt seem to work keep getting false for all of them.
      Code:
      public class CompareStrings { 
      public static boolean match(String x, String y, boolean wildCard ) {
      	//Both strings are empty
      	if((x.length() == 0) &&(y.length()==0))
      		{
      			return true;
      		}
      	//Second string is empty and there is wildCard character
      	if(y.length() == 0 && wildCard)
      		{
      			return true;
      		}
      	if(x.length() == 0 && y.length() > 0 && y.charAt(0) == '*')
      	{
      		String newY = y.replace('0','1');
      		return match(x, newY, true);
      	}
      	//one string is empty, and the second one is not
      	if(x.length()* y.length()== 0)
      	{
      		return false;
      	}
      	if(wildCard)
      	{
      		String newX = x.replace('0','1');
      		if(match(newX,y,true) || match(newX,y, false))
      		{
      			return true;
      		}
      	}
      	else{
      		if(y.charAt(0) == '*')
      		{
      			String newY = y.replace('0','1');
      			if(match(x,newY,true)|| match(x,newY,false))
      			{
      				return true;
      			}
      		}
      		else{
      			if(x.charAt(0) == y.charAt(0))
      			{
      				String newX = x.replace('0','1');
      				String newY = y.replace('0','1');
      				return match(newX, newY, false);
      			}
      			else{
      				return false;
      			}
      		}
      	}
      	return false;	
      }
      	
      public static void main(String args[]) { 
       System.out.println(match("hello", "hello.")); // should return false
       System.out.println(match("hello", "jello")); // should return false
       System.out.println(match("hello", "h@llo")); // should return true
       System.out.println(match("hello", "h@@@@")); // should return true
       System.out.println(match("hello", "h*")); // should return true
       System.out.println(match("hello", "*l*")); // should return true
       System.out.println(match("anyString", "*")); // should return true
       System.out.println(match("help", "h@@@@")); // should return false
       System.out.println(match("help", "h*")); // should return true
       System.out.println(match("help", "*l*")); // should return true
       System.out.println(match("help", "*l*p")); // should return true
       System.out.println(match("help", "h@llo")); // should return false
       System.out.println(match("", "*")); // should return true
       System.out.println(match("", "***")); // should return true
       System.out.println(match("", "@")); // should return false
       } 
      }

      Comment

      • JennaCoder
        New Member
        • Mar 2013
        • 5

        #4
        I want to compare the string using recursion also a wild card for example The matching process should allow "wild cards". A '@' character will match with any other single character and a '*' character will match with 0 or more characters of any type.

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          Why use recursion when iteration is simpler?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Why are you replacing 0 with 1 in the Strings? For a recursive compare you need to add an index to your function. Then you run through the strings comparing positions 0 to N in the strings while comparing stringX.charAt( index) and stringY.charAt( index).

            Comment

            • JennaCoder
              New Member
              • Mar 2013
              • 5

              #7
              I need to know how to do it with recursion for a test,:(

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Start with a normal compare using the method I mentioned above, then when you get it working add the wildcards feature.

                Comment

                • JennaCoder
                  New Member
                  • Mar 2013
                  • 5

                  #9
                  I did this but kept getting un correct result can someone help me fix it up?

                  Code:
                  public class CompareStrings { 
                  public static boolean match(String x, String y) {
                      return match(x.toCharArray(), y.toCharArray(), x.length() - 1, y.length() - 1);
                  }
                  
                  private static boolean match(char[] x, char[] y, int xPosition, int yPosition) {
                      if (xPosition < 0 || yPosition < 0) {
                          return false;
                      }
                      if (xPosition == 0 && yPosition == 0) {
                          return true;
                      }
                      if (x[xPosition] == y[yPosition] || x[xPosition] == '@') {
                          return match(x, y, xPosition - 1, yPosition - 1);
                      }
                      if (x[xPosition] == '*') {
                          if (x[xPosition - 1] == '@') {
                              /* @* => @ matter of taste. Sure there are counter examples. */
                              return match(x, y, xPosition - 2, yPosition - 1);
                          }
                          final int newYPosition = String.valueOf(y).lastIndexOf(x[xPosition - 1]);
                          if (newYPosition >= 0) {
                              return match(x, y, xPosition - 1, newYPosition);
                          }
                      }
                      return false;
                  }
                  	
                  public static void main(String args[]) { 
                   System.out.println(match("hello", "hello.")); // should return false
                   System.out.println(match("hello", "jello")); // should return false
                   System.out.println(match("hello", "h@llo")); // should return true
                   System.out.println(match("hello", "h@@@@")); // should return true
                   System.out.println(match("hello", "h*")); // should return true
                   System.out.println(match("hello", "*l*")); // should return true
                   System.out.println(match("anyString", "*")); // should return true
                   System.out.println(match("help", "h@@@@")); // should return false
                   System.out.println(match("help", "h*")); // should return true
                   System.out.println(match("help", "*l*")); // should return true
                   System.out.println(match("help", "*l*p")); // should return true
                   System.out.println(match("help", "h@llo")); // should return false
                   System.out.println(match("", "*")); // should return true
                   System.out.println(match("", "***")); // should return true
                   System.out.println(match("", "@")); // should return false
                   } 
                  }

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    You should try to find out what's wrong with the code by putting printlns to see the values of variables. Also, you need to think about the logic first before writing anything on the compiler. I don't see why you are putting x and y positions at this stage. First, get a basic recursive compare to work, it only needs to take the two strings and the current position we are comparing at. You call it with zero the first time and move up through the strings comparing characters at the same locations.

                    Comment

                    Working...