Skip to main content
edited title
Link
Tunaki
  • 9.3k
  • 1
  • 31
  • 46

Given 2 strings, consider all the substrings within them Determining if two Strings have common subtrings of a given length n. n will be <= 1. Returns true if there are any such substrings in both stringsJava

added 46 characters in body
Source Link
Thor
  • 627
  • 1
  • 9
  • 14

I know as a matter of fact that there are many areas upon which my code could be improved. However, rather than asking other people to correct my code, I would much prefer to be given hints or general principles as to how my code could be improved. I think Ithis way, me, and everyone that read this post could get more out of this exercise this way.

I know as a matter of fact that there are many areas upon which my code could be improved. However, rather than asking other people to correct my code, I would much prefer to be given hints or general principles as to how my code could be improved. I think I could get more out of this exercise this way.

I know as a matter of fact that there are many areas upon which my code could be improved. However, rather than asking other people to correct my code, I would much prefer to be given hints or general principles as to how my code could be improved. I think this way, me, and everyone that read this post could get more out of this exercise this way.

Source Link
Thor
  • 627
  • 1
  • 9
  • 14

Given 2 strings, consider all the substrings within them of length n. n will be <= 1. Returns true if there are any such substrings in both strings

Problem:

Given 2 strings, consider all the substrings within them of length len. Len will be 1 or more. Returns true if there are any such substrings which appear in both strings. Compute this in linear time using a HashSet.

I know as a matter of fact that there are many areas upon which my code could be improved. However, rather than asking other people to correct my code, I would much prefer to be given hints or general principles as to how my code could be improved. I think I could get more out of this exercise this way.

So could someone please provide me with some comments or suggestions? Critics of any level and kind are welcomed! Feel free to pick apart my code!

Class:

import java.util.HashSet;
import java.util.Set;

// CS108 HW1 -- String static methods
public class StringCode {


    public static boolean stringIntersect(String firstString, String secondString, int lengthOfSubstring) {
        
        boolean sameSubstringInBothStrings = false;
        
        Set<String> setOne = StringCode.getSubstringsSet(firstString, lengthOfSubstring);
        Set<String> setTwo = StringCode.getSubstringsSet(secondString, lengthOfSubstring);
        
        
        //compare setOne and setTwo to find out if there is any matching elements
        outerForLoop:
        for(String aSubstringInSetOne : setOne){
            for(String aSubstringInSetTwo : setTwo){
                if(aSubstringInSetOne.equals(aSubstringInSetTwo)){
                    sameSubstringInBothStrings = true;
                    break outerForLoop;
                }
            }
        }
        
        return sameSubstringInBothStrings;
    }
    
    static Set<String> getSubstringsSet(String aString, int lengthOfSubstring){
        Set<String> setOfSubstrings = new HashSet<>();
        
        if(aString.length() < lengthOfSubstring){
            return setOfSubstrings;
        }
        
        if(aString.length() == lengthOfSubstring){
            setOfSubstrings.add(aString);
            return setOfSubstrings;
        }
        
        char[] charArray = aString.toCharArray();
        
        //starting from the first index, going through aString 
        for(int i = 0; i <= aString.length() - lengthOfSubstring; i++){
            
            StringBuilder sb = new StringBuilder();
            
            //add each substring of length (lengthOfSubstring) to the setOfSubstrings. 
            for(int j = 0; j < lengthOfSubstring; j++){
                sb.append(charArray[i + j]);
            }
            
            setOfSubstrings.add(sb.toString());
        }
        
        return setOfSubstrings;
    }

}

Test:

import assign1.StringCode;
import static org.junit.Assert.*;
import org.junit.Test;

public class StringCodeTest {

    
    @Test
    public void testStringIntersectNormalStrings(){
        assertEquals(true, StringCode.stringIntersect("abcdickefg" , "zyxqdick", 4));
    }
    
    @Test
    public void testStringIntersectNormalStrings2(){
        assertEquals(false, StringCode.stringIntersect("abcdef", "zzzzzz", 1));
    }
    
    @Test
    public void testEmptyString(){
        assertEquals(false, StringCode.stringIntersect("abc", "", 2));
    }
    
    @Test
    public void testLengthOfSubstringGreaterThanTheStringItself(){
        assertEquals(false, StringCode.stringIntersect("abc", "xyz", 5));
    }
    
    @Test 
    public void testTwoEmptyStrings(){
        assertEquals(false, StringCode.stringIntersect("", "", 2));
    }

}