Skip to content

Commit

Permalink
Merge branch 'Spectrum-CETB:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
heisenberg070 authored Oct 4, 2022
2 parents ebc5125 + c3c79f3 commit dde8dff
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
39 changes: 39 additions & 0 deletions coding_freshmen/C/heySwayam/pattern_finder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdio.h>
#include <string.h>
void search(char pat[100], char txt[100])
{
int pat_len = strlen(pat);
int txt_len = strlen(txt);
int sum, n = 0;
for (int i = 0; i < txt_len; i++)
{
if (txt[i] == pat[0])
{
sum = 0;
for (int j = 0; j < (pat_len - 1); j++)
{
if (txt[i + j + 1] == pat[j + 1])
{
sum++;
}
}

if (sum == 2)
{
n++;
printf("Pattern found at index %d\n",i);
}
}
}
}
int main()
{
char pat[100];
char txt[100];
printf("Enter the pattern\n");
gets(pat);
printf("Enter the text\n");
gets(txt);
search(pat, txt);
return 0;
}
21 changes: 21 additions & 0 deletions coding_freshmen/C/heySwayam/pattern_finder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# <Title of the Problem>
Pattern Finder

# Problem Explanation 🚀
Given a text txt[0. . .n-1] and a pattern pat[0. . .m-1].

Write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[]..

# Your logic 🤯
* Approach: Created two arrays to hold the two strings and compared the array one by one and get the index
* Own test cases if any
* Code Structure and Libraries used

# Time Complexity and Space Complexity
```cpp
Example

Time Complexity -> O(n^2)
Space Complexity -> O(1)

```

0 comments on commit dde8dff

Please sign in to comment.