diff --git a/String Algorithms/Reverse Factor Algorithm/Program.c b/String Algorithms/Reverse Factor Algorithm/Program.c new file mode 100644 index 00000000..22352af3 --- /dev/null +++ b/String Algorithms/Reverse Factor Algorithm/Program.c @@ -0,0 +1,56 @@ +#include +#include + +// 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; +} \ No newline at end of file diff --git a/String Algorithms/Reverse Factor Algorithm/README.md b/String Algorithms/Reverse Factor Algorithm/README.md new file mode 100644 index 00000000..449ba214 --- /dev/null +++ b/String Algorithms/Reverse Factor Algorithm/README.md @@ -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. \ No newline at end of file