Rabin-Karp

internal override SearchResult Search(string text, string pattern) {
	if (pattern.Length > text.Length) return SearchResult.Empty();

	SearchResult searchResult = new SearchResult() {
		Matches = new List < Match > ()
	};
	int patternHashValue = pattern.GetHashCode();
	for (int index = 0; index + pattern.Length < text.Length; index++) {
		string indexString = text.Substring(index, pattern.Length);
		int indexHashValue = indexString.GetHashCode();
		if (patternHashValue == indexHashValue) if (string.CompareOrdinal(pattern, indexString) == 0) searchResult.AddMatch(new Match() {
			Position = index
		});
	}

	return searchResult;
}
0 0 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments