Skip to content
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

Naive Searching algorithm in string added #672

Merged
merged 24 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
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
43 changes: 43 additions & 0 deletions Algorithms/String Based Algorithms/NaiveSearching/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Naive Searching Algorithm
Naive searching algorithm is one of the simplest algorithms to find if a given pattern is present in a string or not. The concept is pretty simple and we dont have to precompute anything, the drawback being it is very costly in terms of time complexity.

### Sample Code
```
def naive_search(text, pattern):
"""
Naive string searching algorithm.

Parameters:
text (str): The input string to search within.
pattern (str): The pattern to search for.

Returns:
list: A list of indices where the pattern is found in the text.
"""
indices = []
text_length = len(text)
pattern_length = len(pattern)

for i in range(text_length - pattern_length + 1):
match = True
for j in range(pattern_length):
if text[i + j] != pattern[j]:
match = False
break

if match:
indices.append(i)

return indices

# Example usage
text = "ababcababcabc"
pattern = "abc"
result = naive_search(text, pattern)
print("Pattern found at indices:", result)

```

### Result

Pattern found at indices: [2, 7, 10]
10 changes: 5 additions & 5 deletions Algorithms/String Based Algorithms/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

String-based algorithms are computational techniques used to perform various operations on strings, such as searching for a substring, pattern matching, and text processing. These algorithms are designed to process and analyze strings efficiently, and their performance is often measured in terms of time complexity, which indicates how the algorithm's runtime scales with the size of the input string.

1. **Naive String Search:**
1. [**Naive String Search:**](NaiveSearching/readme.md)
- Time Complexity: O(m * n), where m is the length of the pattern and n is the length of the text.
- Description: The naive string search algorithm compares each character of the pattern with each character of the text, sliding the pattern one position to the right until a match is found or the end of the text is reached. It has a quadratic time complexity.

2. [**Knuth–Morris–Pratt Algorithm (KMP):**](KMP/readme.md)
3. [**Knuth–Morris–Pratt Algorithm (KMP):**](KMP/readme.md)
- Time Complexity: O(m + n), where m is the length of the pattern and n is the length of the text.
- Description: The KMP algorithm is used for efficient string matching by avoiding unnecessary character comparisons through the use of a prefix function. It preprocesses the pattern to find a partial match between the pattern and itself and utilizes this information to skip unnecessary comparisons during text matching.

3. **Boyer-Moore Algorithm:**
4. **Boyer-Moore Algorithm:**
- Time Complexity: Best-case O(n/m), Worst-case O(m * n), where m is the length of the pattern and n is the length of the text.
- Description: The Boyer-Moore algorithm is a popular string-searching algorithm that uses two heuristics to skip large portions of the text during searching. It preprocesses the pattern and utilizes a "bad character" and "good suffix" rule to determine how much the pattern can be shifted during the search.

4. **String Hashing:**
5. **String Hashing:**
- Time Complexity: O(n + m), where n is the length of the text and m is the length of the pattern.
- Description: String hashing algorithms, such as Rabin-Karp, use hash functions to quickly compare patterns and substrings of a text. The Rabin-Karp algorithm, in particular, is based on hashing and is useful for pattern matching. It calculates hash values for the pattern and text substrings, allowing for fast comparisons. [Learn more about Rabin-Karp](RabinKarp/readme.md)

5. **Suffix Trie:**
6. **Suffix Trie:**
- Time Complexity: O(m), where m is the length of the pattern.
- Description: A suffix trie is a data structure used for advanced string matching and text processing. It preprocesses the text by building a trie containing all the suffixes of the text. Pattern matching can then be done efficiently by traversing the trie based on the pattern. Suffix tries can have high space complexity but offer fast pattern matching.
Loading