-
Notifications
You must be signed in to change notification settings - Fork 30
first assignment #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Scanner; | ||
public class Solution { | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
String text = sc.nextLine(); | ||
int q = sc.nextInt(); | ||
ArrayList<Integer> res = new ArrayList<>(); | ||
sc.nextLine(); | ||
while(q-- > 0) { | ||
String pat = sc.nextLine(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. give proper names to the local variables. |
||
for(int i=0; i<text.length(); i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you are iterating whole text string on every pattern (O(n*n) in worst case). the run time will be very high on large inputs. Please try to fix it in single scan of text string. |
||
int j, k = i; | ||
for(j=0; j<pat.length() && k < text.length(); j++) { | ||
if(text.charAt(k) == pat.charAt(j)) | ||
k++; | ||
else | ||
break; | ||
} | ||
if(j == pat.length()) | ||
res.add(i); | ||
} | ||
} | ||
Collections.sort(res); | ||
for(Integer i : res) | ||
System.out.print(i + " "); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use a separate method to get the output instead of writing the logic in the main method & run formatter to give proper indentation in your code.