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

#1398 Added Reverse Factor Algorithm #1435

Merged
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
56 changes: 56 additions & 0 deletions String Algorithms/Reverse Factor Algorithm/Program.c
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;
}
49 changes: 49 additions & 0 deletions String Algorithms/Reverse Factor Algorithm/README.md
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.