Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions AayushRawat.java
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) {
Copy link
Contributor

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.

String pat = sc.nextLine();
Copy link
Contributor

Choose a reason for hiding this comment

The 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++) {

Choose a reason for hiding this comment

The 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 + " ");
}

}