-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1435 from pratheekv39/feature/reverse_factor_algo…
…rithm #1398 Added Reverse Factor Algorithm
- Loading branch information
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
// Function to reverse a string | ||
void reverseString(char* str) { | ||
int n = strlen(str); | ||
for (int i = 0; i < n / 2; i++) { | ||
char temp = str[i]; | ||
str[i] = str[n - i - 1]; | ||
str[n - i - 1] = temp; | ||
} | ||
} | ||
|
||
// Function to check if a substring is a factor of the main string | ||
int isFactor(char* sub, char* str) { | ||
int subLen = strlen(sub); | ||
int strLen = strlen(str); | ||
|
||
for (int i = 0; i <= strLen - subLen; i++) { | ||
int j; | ||
for (j = 0; j < subLen; j++) { | ||
if (str[i + j] != sub[j]) | ||
break; | ||
} | ||
if (j == subLen) | ||
return i; // Substring found at index i | ||
} | ||
|
||
return -1; // Substring not found | ||
} | ||
|
||
// Function to implement the Reverse Factor Algorithm | ||
void reverseFactor(char* pattern, char* text) { | ||
// Reverse the pattern | ||
reverseString(pattern); | ||
|
||
// Check if the reversed pattern is a factor of the text | ||
int index = isFactor(pattern, text); | ||
|
||
if (index != -1) | ||
printf("Reversed pattern found at index %d\n", index); | ||
else | ||
printf("Reversed pattern not found\n"); | ||
} | ||
|
||
int main() { | ||
char text[] = "ABCDABCDABC"; | ||
char pattern[] = "ABC"; | ||
|
||
printf("Text: %s\n", text); | ||
printf("Pattern: %s\n", pattern); | ||
|
||
reverseFactor(pattern, text); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Reverse Factor Algorithm | ||
|
||
## Description | ||
|
||
The Reverse Factor Algorithm is a string-searching algorithm that checks if the reverse of a given pattern appears as a substring within a text. It can be applied in various situations where reverse-pattern matching is required, such as palindrome detection or specific text searching. | ||
|
||
### Problem Definition | ||
|
||
Given: | ||
- A text string `T` of length `n` | ||
- A pattern string `P` of length `m` | ||
|
||
Objective: | ||
- Find if the reverse of pattern `P` is present in text `T` and report its position. | ||
|
||
### Algorithm Overview | ||
|
||
1. **Reverse the Pattern**: Reverse the characters of the pattern string `P`. | ||
2. **Search for the Reversed Pattern**: | ||
- Slide the reversed pattern over the text one character at a time. | ||
- For each window of length `m` in the text, compare it with the reversed pattern. | ||
3. **Report Matches**: If the reversed pattern matches a substring in the text, report the starting index. | ||
|
||
### Key Features | ||
|
||
- Simple string comparison to check for reversed pattern matches | ||
- Applicable for reverse pattern detection in text or similar tasks | ||
- Time complexity is linear with respect to the length of the text for most cases | ||
|
||
### Time Complexity | ||
|
||
- Best and Average Case: O(n) | ||
- Worst Case: O(n * m), occurs when there are many partial matches in the text. | ||
|
||
### Space Complexity | ||
|
||
O(m), where `m` is the length of the pattern, as we store a reversed copy of the pattern. | ||
|
||
## Implementation | ||
|
||
The C implementation of the Reverse Factor Algorithm involves: | ||
|
||
1. A function to reverse the given pattern string. | ||
2. A function to check if the reversed pattern is a substring of the text. | ||
3. A main function that demonstrates the usage of the algorithm. | ||
|
||
## Usage | ||
|
||
Compile the program and run it. The example in the `main` function shows how the Reverse Factor Algorithm works by finding if the reverse of a given pattern is present in a text string. |