This is the complete solution to find the substrings of a given String. This solution is designed to solve the problem using an interface, super class and three derived classes implementing the interface.
package pack1;
import java.util.Set;
public interface SubstrsIntf {
/* Define the Functional interface with SAM 'ParseTheWord' */
public Set<String> ParseTheWord(String inputWord);
/* Define the Static method 'displayMsg' */
public static void displayMsg(Set<String> finalSubsetStrs, String inpStrVal) {
System.out.println("All the unique substrings from the given String="+inpStrVal+" are below");
System.out.println(finalSubsetStrs);
}
}
package pack1;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Logger;
class StringInput {
private String inpStrVal;
/* Declare the logger object for the class to display the log messages */
static Logger logger = Logger.getLogger(StringInput.class.getName());
public boolean validateInputs(String inpWord) {
/* Fetch the class object's name from which this method is called */
String cName = Thread.currentThread().getStackTrace()[2].getClassName();
String mName = "validateInputs()";
logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
/* Validating the function parameters using Optional String data type */
Optional<String> strInput = Optional.ofNullable(inpWord);
/*
* Fetch the value of the Optional String using 'isPresent' condition check.
* If the value is not null, then use the 'get' method otherwise simply
* return 'false' to stop the program execution without proceeding further.
*/
if (strInput.isPresent()) {
setInpStrVal(strInput.get());
} else {
return false;
}
logger.info("The provided Input validated Successfully");
return true;
}
/* Define getter and setters for inpStrVal String variable */
public String getInpStrVal() {
return inpStrVal;
}
public void setInpStrVal(String inpStrVal) {
this.inpStrVal = inpStrVal;
}
}
class Substrs_Using_String extends StringInput implements SubstrsIntf {
public Set<String> ParseTheWord(String inpWord) {
/* Fetch the class object's name from which this method is called */
String cName = Thread.currentThread().getStackTrace()[2].getClassName();
String mName = "ParseTheWord";
logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
/* Define a Set of data type Strings */
Set<String> finalSubsetStrs = new HashSet<String> ();
if (validateInputs(inpWord)) {
int sizeOfSubstr=1,beginIdx=0, endIdx=1;
//Iterate the loop until the substring's length is equals with length of the actual string
while (sizeOfSubstr <= getInpStrVal().length()) {
//Iterate the substring sized loop until the subscript less than or equals the actual length
while (beginIdx < getInpStrVal().length() && endIdx <= getInpStrVal().length())
finalSubsetStrs.add(getInpStrVal().substring(beginIdx++, endIdx++));
sizeOfSubstr++; // Increment size of sub string for the next iteration
beginIdx=0;
endIdx=sizeOfSubstr;
}
} else
logger.info("The given Input String is null");
SubstrsIntf.displayMsg(finalSubsetStrs,inpWord);
return finalSubsetStrs;
}
}
class Substrs_WithoutUsing_SubstringMethod extends StringInput implements SubstrsIntf {
public Set<String> ParseTheWord(String inpWord) {
/* Fetch the class object's name from which this method is called */
String cName = Thread.currentThread().getStackTrace()[2].getClassName();
String mName = "ParseTheWord";
logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
/* Define a Set of type Strings */
Set<String> finalSubsetStrs = new HashSet<String> ();
if (validateInputs(inpWord)) {
int sizeOfSubstr=1,beginIdx=0, endIdx=1;
//Iterate the loop until the substring's length is equals with length of the actual string
while (sizeOfSubstr <= getInpStrVal().length()) {
//Iterate the substring sized loop until the subscript less than or equals the actual length
while (beginIdx < getInpStrVal().length() && endIdx <= getInpStrVal().length())
finalSubsetStrs.add(prepSubstr(beginIdx++, endIdx++, getInpStrVal()));
sizeOfSubstr++; // Increment size of sub string for the next iteration
beginIdx=0;
endIdx=sizeOfSubstr;
}
} else
logger.info("The given Input String is null");
SubstrsIntf.displayMsg(finalSubsetStrs,inpWord);
return finalSubsetStrs;
}
private static String prepSubstr(int bIdx, int eIdx, String inpWord) {
StringBuilder formStr=new StringBuilder ();
for(int i=bIdx;i<=eIdx-1;i++ )
formStr.append(inpWord.charAt(i));
return formStr.toString();
}
}
class Substrs_Using_Lambda extends StringInput {
SubstrsIntf ss = ((strVal) -> {
int sizeOfSubstr = 1, beginIdx = 0, endIdx = 1;
Set<String> SubsetStrs = new HashSet<String>();
/* Iterate the loop until the substring's length is equals with length of the
actual string */
while (sizeOfSubstr <= strVal.length()) {
/*Iterate the substring sized loop until the subscript less than or equals the
actual length */
while (beginIdx < strVal.length() && endIdx <= strVal.length())
SubsetStrs.add(strVal.substring(beginIdx++, endIdx++));
/*
* Increment size of sub string 'sizeOfSubstr' by 1, reset 'beginIdx' and assign
* next sized substring to 'endIdx' for the next iteration
*/
sizeOfSubstr++;
beginIdx = 0;
endIdx = sizeOfSubstr;
}
return SubsetStrs;
});
public Set<String> ParseTheWord(String inpWord) {
/* Fetch the class object's name from which this method is called */
String cName = Thread.currentThread().getStackTrace()[2].getClassName();
String mName = "ParseTheWord";
logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
/* Define a Set of type Strings */
Set<String> finalSubsetStrs = new HashSet<String> ();
if (validateInputs(inpWord)) {
/* Call the Single AbStract Method 'ParseTheWord' of the interface*/
finalSubsetStrs = ss.ParseTheWord(getInpStrVal());
} else
logger.info("The given Input String is null");
SubstrsIntf.displayMsg(finalSubsetStrs,inpWord);
return finalSubsetStrs;
}
}
public class SubstrsJ8 {
public static void main(String args[]) {
/*
* create the class object Substr_Using_String, and call the method 'ParseTheWord'
*/
Substrs_Using_String SubUStr = new Substrs_Using_String();
SubUStr.ParseTheWord("TONY");
SubUStr.ParseTheWord(null);
SubUStr.ParseTheWord("");
/*
* create the class object Substrs_WithoutUsing_SubstringMethod, and call the method 'ParseTheWord'
*/
Substrs_WithoutUsing_SubstringMethod SubUStrWithout = new Substrs_WithoutUsing_SubstringMethod();
SubUStrWithout.ParseTheWord("KERR");
SubUStrWithout.ParseTheWord(null);
SubUStrWithout.ParseTheWord("");
/*
* create the class object Substrs_Using_Lambda, and call the method 'ParseTheWord'
*/
Substrs_Using_Lambda SubUStrUL = new Substrs_Using_Lambda();
SubUStrUL.ParseTheWord("3qW&$#");
SubUStrUL.ParseTheWord(null);
SubUStrUL.ParseTheWord("");
}
}
OUTPUT:
Jun 11, 2023 3:38:37 PM pack1.Substrs_Using_String ParseTheWord
INFO: This method 'ParseTheWord' called from the Class 'pack1.SubstrsJ8'
Jun 11, 2023 3:38:37 PM pack1.StringInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Substrs_Using_String'
Jun 11, 2023 3:38:37 PM pack1.StringInput validateInputs
INFO: The provided Input validated Successfully
All the unique substrings from the given String=TONY are below
[TONY, TON, T, Y, TO, NY, ONY, N, O, ON]
Jun 11, 2023 3:38:37 PM pack1.Substrs_Using_String ParseTheWord
INFO: This method 'ParseTheWord' called from the Class 'pack1.SubstrsJ8'
Jun 11, 2023 3:38:37 PM pack1.StringInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Substrs_Using_String'
Jun 11, 2023 3:38:37 PM pack1.Substrs_Using_String ParseTheWord
INFO: The given Input String is null
All the unique substrings from the given String=null are below
[]
Jun 11, 2023 3:38:37 PM pack1.Substrs_Using_String ParseTheWord
INFO: This method 'ParseTheWord' called from the Class 'pack1.SubstrsJ8'
Jun 11, 2023 3:38:37 PM pack1.StringInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Substrs_Using_String'
Jun 11, 2023 3:38:37 PM pack1.StringInput validateInputs
INFO: The provided Input validated Successfully
All the unique substrings from the given String= are below
[]
Jun 11, 2023 3:38:37 PM pack1.Substrs_WithoutUsing_SubstringMethod ParseTheWord
INFO: This method 'ParseTheWord' called from the Class 'pack1.SubstrsJ8'
Jun 11, 2023 3:38:37 PM pack1.StringInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Substrs_WithoutUsing_SubstringMethod'
Jun 11, 2023 3:38:37 PM pack1.StringInput validateInputs
INFO: The provided Input validated Successfully
All the unique substrings from the given String=KERR are below
[RR, R, ERR, E, KER, KERR, KE, K, ER]
Jun 11, 2023 3:38:37 PM pack1.Substrs_WithoutUsing_SubstringMethod ParseTheWord
INFO: This method 'ParseTheWord' called from the Class 'pack1.SubstrsJ8'
Jun 11, 2023 3:38:37 PM pack1.StringInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Substrs_WithoutUsing_SubstringMethod'
Jun 11, 2023 3:38:37 PM pack1.Substrs_WithoutUsing_SubstringMethod ParseTheWord
INFO: The given Input String is null
All the unique substrings from the given String=null are below
[]
Jun 11, 2023 3:38:37 PM pack1.Substrs_WithoutUsing_SubstringMethod ParseTheWord
INFO: This method 'ParseTheWord' called from the Class 'pack1.SubstrsJ8'
Jun 11, 2023 3:38:37 PM pack1.StringInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Substrs_WithoutUsing_SubstringMethod'
Jun 11, 2023 3:38:37 PM pack1.StringInput validateInputs
INFO: The provided Input validated Successfully
All the unique substrings from the given String= are below
[]
Jun 11, 2023 3:38:37 PM pack1.Substrs_Using_Lambda ParseTheWord
INFO: This method 'ParseTheWord' called from the Class 'pack1.SubstrsJ8'
Jun 11, 2023 3:38:37 PM pack1.StringInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Substrs_Using_Lambda'
Jun 11, 2023 3:38:37 PM pack1.StringInput validateInputs
INFO: The provided Input validated Successfully
All the unique substrings from the given String=3qW&$# are below
[qW&, #, $, &$#, &, qW, W&$#, 3qW&$#, W&, qW&$, q, 3, W&$, 3qW&$, 3qW&, W, 3qW, 3q, &$, $#, qW&$#]
Jun 11, 2023 3:38:37 PM pack1.Substrs_Using_Lambda ParseTheWord
INFO: This method 'ParseTheWord' called from the Class 'pack1.SubstrsJ8'
Jun 11, 2023 3:38:37 PM pack1.StringInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Substrs_Using_Lambda'
All the unique substrings from the given String=null are below
[]
Jun 11, 2023 3:38:37 PM pack1.Substrs_Using_Lambda ParseTheWord
INFO: The given Input String is null
Jun 11, 2023 3:38:37 PM pack1.Substrs_Using_Lambda ParseTheWord
INFO: This method 'ParseTheWord' called from the Class 'pack1.SubstrsJ8'
Jun 11, 2023 3:38:37 PM pack1.StringInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Substrs_Using_Lambda'
Jun 11, 2023 3:38:37 PM pack1.StringInput validateInputs
INFO: The provided Input validated Successfully
All the unique substrings from the given String= are below
[]
The link to the JUnit5 test for this solution is here – https://atomic-temporary-185308886.wpcomstaging.com/2023/06/12/substrings-unittest/

Leave a comment