Answered step by step
Verified Expert Solution
Question
1 Approved Answer
this is a recursive version for the first part, the second part must have a faster execution time and without recursion is suggested. I require
this is a recursive version for the first part, the second part must have a faster execution time and without recursion is suggested.
I require help with a version 2, one that does not use recursion, in Java. Thank you:)
import java.io.*; import java.util.*; public class RecursiveVersion1 { public static void main(String[] args) throws Exception String ans=""; System.out.println("Enter Long String: "); Scanner s=new Scanner(System.in); String longStr=s.nextLine(); while(true){ System.out.println("Enter Short String or -1 for exit"); Long startTime = System.nano Time(); String shortStr=s.nextLine(); if(shortStr.equals("-1")) { break; }// will end the program and exit if no more short strings need to be checked FileWriter fw=new FileWriter( fileName: "output.txt", append: true); // for the output Permu( shortStr, ans, longStr, fw); long endTime = System.nanoTime(); FileWriter fwl=new Filewriter fileName: "timings.txt", append: true); // for time observation (in seconds) fwi.writel str: longStr+""+shortStr+""+"Execution time: : "+""+(endTime-start Time)/1000000000.0+" s "); // converting it to seconds instead of nanoseconds fwi.close(); fw.close(); 31 static void Permu(String str, String ans, String longStr, Filewriter fw) throws Exception 36 37 if (str. Length() == 0) { int p=longstr.indexOf(ans); System.out.println(ans); fw.writestr: ans+" "); if(p!=-1){ System.out.println("Found one match: "+ans+" is in "+longStr+" at location "+p); fw.write( str: "Found one match: "+ans+" is in "+LongStr+" at location "+p+" "); return; for (int i = b; iStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started