newhere
BAN USER- 0of 0 votes
AnswersI have given a Long Sentence and some words(to be searched in the sentence), i have to find the smallest part of the sentence which contains all the words to be Searched in that Sentence and print that part.
I have tried it, 1. First get all locations(indexes) of all words from the given sentence. 2. then try to find smallest part from these indexes of words.
But i am having problem implementing 2nd part . So i want some advice for it or if you suggest any other algorithm which can make it fast.
Example: If i have a String > "Hello, my name is undefined. but your name is not undefined." and i have to search word [is, undefined] then the answer should be printed is: is undefined //which is taken from the sentence 1st and we can not print from second sentence as it is " is not undefined" because it is not shortest.import java.util.*; import java.io.*; public class ShotestSubSegment2 { static SearchStr[] search; static String copystr; public static void main(String s[]) { try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = in.readLine(); copystr = str.substring(0).toLowerCase(); int k = Integer.parseInt(in.readLine()); search = new SearchStr[k]; for(int i=0;i<k;i++) { search[i] = new SearchStr(in.readLine().toLowerCase()); getIndicesOf(search[i]); if(search[i].noOfElements()==0) { System.out.println("No Segments Found"); return; } } searchSmallestPart();//Dont getting Idea Of this } catch(Exception x){} } public static void getIndicesOf(SearchStr searchS) { String searchStr = searchS.getName(); int startIndex = 0, searchStrLen=0; int index; searchStr = searchStr.toLowerCase(); searchStrLen = searchStr.length(); while ((index = copystr.indexOf(searchStr, startIndex)) > -1) { searchS.add(index); startIndex = index + searchStrLen; } }
}
- newhere in United States| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm Java
hey can anyone tell algorithm ...
- newhere June 28, 2012how it can be soved using dp.