Java > Recursion-1 > strDist (CodingBat Solution)

Problem:

Given a string and a non-empty substring sub, compute recursively the largest substring which starts and ends with sub and return its length.

strDist("catcowcat", "cat") → 9
strDist("catcowcat", "cow") → 3
strDist("cccatcowcatxx", "cat") → 9


Solution:

public int strDist(String str, String sub) {
  return func(str, sub).length();  
}

private String func(String str, String sub) {
  int strlen = str.length();
  int sublen = sub.length();
  if (str.equals("")) return str;
  if (str.startsWith(sub)) {
    if (str.substring(strlen-sublen, strlen).equals(sub))
      return str;
    else
      return func(str.substring(0, strlen-1), sub);
  }
  else
    return func(str.substring(1), sub);
}


13 comments :

  1. public int strDist(String str, String sub) {
    if(str.length()==sub.length()&&str.equals(sub))return str.length();
    else if(str.length()<=sub.length())return 0;
    else if(str.substring(0,sub.length()).equals(sub)){
    if(str.length()>sub.length()&&str.substring((str.length()-sub.length()),str.length()).equals(sub))
    return str.length();
    else
    return strDist(str.substring(0,str.length()-1),sub);
    }
    return strDist(str.substring(1),sub);
    }

    ReplyDelete
  2. public int strDist(String str, String sub) {
    if (!str.equals(sub) && str.length() <= sub.length()) return 0;
    if (!str.startsWith(sub)) return strDist(str.substring(1, str.length()), sub);
    if (!str.endsWith(sub)) return strDist (str.substring(0, str.length()-1), sub);
    return str.length();

    }

    ReplyDelete
    Replies
    1. public int strDist(String str, String sub) {
      if (str.length() < sub.length()) return 0;
      if (!str.startsWith(sub)) return strDist(str.substring(1, str.length()), sub);
      if (!str.endsWith(sub)) return strDist (str.substring(0, str.length()-1), sub);
      return str.length();
      }

      Delete
  3. public int strDist(String str, String sub) {

    if (str.startsWith(sub) && str.endsWith(sub)){
    return str.length();
    }
    else if (!str.startsWith(sub)){
    if (str.length() > 1)
    return strDist(str.substring(1), sub);
    else
    return 0;
    }
    else if (!str.endsWith(sub)){
    if (str.length() > 0)
    return strDist(str.substring(0, str.length()-1), sub);
    else
    return 0;
    }
    else{
    return 0;
    }
    }

    ReplyDelete
  4. public int strDist(String str, String sub) {
    if(str.equals(sub))
    return sub.length();
    else if(str.length() < sub.length())
    return 0;
    else if(str.substring(0,sub.length()).equals(sub))
    {
    if(str.substring(str.length() - sub.length()).equals(sub))
    return str.length();
    else return strDist(str.substring(0,(str.length() -1)),sub);
    }
    else return strDist(str.substring(1), sub);
    }

    ReplyDelete
  5. I didn't do it recursively but it still works!!!!

    public int strDist(String str, String sub) {
    if (!(str.contains(sub))) {
    return 0;
    }
    return str.substring(str.indexOf(sub), str.lastIndexOf(sub)).length() + sub.length();
    }

    ReplyDelete
    Replies
    1. public int strDist(String str, String sub) {
      return str.lastIndexOf(sub) != -1 ? str.lastIndexOf(sub) - str.indexOf(sub) + sub.length() : 0;
      }

      Delete
  6. public int strDist(String str, String sub) {
    int len=str.length();
    int sl=sub.length();
    if(str.equals(""))
    return 0;
    if(str.startsWith(sub)){
    if(str.substring(len-sl,len).equals(sub))
    return len;
    else
    return strDist(str.substring(0,len-1),sub);
    }
    else
    return strDist(str.substring(1),sub);
    }

    ReplyDelete
  7. public int strDist(String str, String sub) {
    if (!(str.contains(sub))) return 0;
    if (!(str.startsWith(sub))) return strDist(str.substring(1), sub);
    if (!(str.endsWith(sub))) return strDist(str.substring(0, str.length() - 1), sub);
    return str.length();
    }

    ReplyDelete
  8. public int strDist(String str, String sub) {

    if (str.length()2){
    return strDist(str.substring(1,str.length()-1), sub);
    }

    return 0;
    }

    ReplyDelete
  9. public int strDist(String str, String sub) {
    if (str.length() < sub.length())
    {
    return 0;
    }
    if (str.substring(0, sub.length()).equals(sub) && str.substring(str.length() - sub.length()).equals(sub))
    {
    return str.length();
    }
    else
    {
    if (!str.substring(0, sub.length()).equals(sub))
    {
    return strDist(str.substring(1), sub);
    }
    else
    {
    return strDist(str.substring(0, str.length() - 1), sub);
    }
    }
    }

    ReplyDelete
  10. public int strDist(String str, String sub) {
    if(str.length() < sub.length()
    || str.indexOf(sub) == -1){
    return 0;
    }
    if(!str.substring(0, sub.length()).equals(sub)){
    return strDist(str.substring(str.indexOf(sub)), sub);
    }
    if(!str.substring(str.length() - sub.length()).equals(sub)){
    return strDist(str.substring(0, str.length() - 1), sub);
    }
    return str.length();
    }

    ReplyDelete
  11. public int strDist(String str, String sub) {
    if (str.length() < sub.length() || str.equals("")) return 0;
    if (str.substring(0, sub.length()).equals(sub)) {
    if (str.substring(str.length()-sub.length(), str.length()).equals(sub)) return str.length();
    else return strDist(str.substring(0, str.length()-1), sub);
    }
    return strDist(str.substring(1), sub);
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact