Skip to content

Solution for String pattern Matching with three algoritms#1467

Merged
RezaSi merged 2 commits intoRezaSi:mainfrom
PopovMarko:challenge-23-marko
Mar 16, 2026
Merged

Solution for String pattern Matching with three algoritms#1467
RezaSi merged 2 commits intoRezaSi:mainfrom
PopovMarko:challenge-23-marko

Conversation

@PopovMarko
Copy link
Contributor

Naive, KMP, RabinKarp implement three algoritms

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 28, 2026

Walkthrough

Adds a new Go source file providing three pattern-matching implementations (Naive, KMP, Rabin–Karp) that operate on rune slices, return starting indices of matches, and include a main with test cases; functions handle empty inputs and pattern-longer-than-text edge cases.

Changes

Cohort / File(s) Summary
Pattern Matching Implementations
challenge-23/submissions/PopovMarko/solution-template.go
Adds a new Go file with three exported functions: NaivePatternMatch, KMPSearch, and RabinKarpSearch. Each works on runes, returns match start indices, includes edge-case handling, and a main with test scaffolding. Implements LPS construction for KMP and rolling-hash+verification for Rabin–Karp.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 I hopped through runes at break of day,
Finding patterns the old-school way,
KMP lends a clever skip,
Rabin’s hash makes matches quick,
Naive hums along — hooray! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a solution with three string pattern-matching algorithms (Naive, KMP, and Rabin-Karp).
Description check ✅ Passed The description is directly related to the changeset, identifying the three algorithms implemented (Naive, KMP, RabinKarp) despite minimal detail.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3


ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a7ac764 and 51aa66d.

📒 Files selected for processing (1)
  • challenge-23/submissions/PopovMarko/solution-template.go

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
challenge-23/submissions/PopovMarko/solution-template.go (2)

44-55: Consider extracting shared preprocessing into a small helper.

Rune conversion and guard checks are duplicated across all three algorithms; factoring this once would reduce maintenance drift.

Also applies to: 83-94, 149-160


143-145: Consider using int64 for rolling-hash arithmetic to support 32-bit platforms.

The rolling-hash computation at lines 28, 33–34, and 52 uses untyped integer constants (base=256, mod=101) with int-typed variables. On 32-bit architectures, expressions like base*(textHash-int(rText[i])*h) can overflow and corrupt matches. No 32-bit targets are currently tested in CI (Go 1.25.0 default is 64-bit), but using int64 improves portability.

🔧 Suggested fix
 func RabinKarpSearch(text, pattern string) []int {
-	const base = 256
-	const mod = 101 // prime number
+	const base int64 = 256
+	const mod int64 = 101 // prime number
 	res := []int{}
 	rText := []rune(text)
 	rPattern := []rune(pattern)
 	lenPattern := len(rPattern)
 	lenText := len(rText)
 	if text == "" || pattern == "" || lenPattern > lenText {
 		return []int{}
 	}
-	var patternHash, textHash, h int
+	var patternHash, textHash, h int64
 	h = 1
 	for i := 0; i < lenPattern-1; i++ {
 		h = (h * base) % mod
 	}
 	for i := range lenPattern {
-		patternHash = (base*patternHash + int(rPattern[i])) % mod
-		textHash = (base*textHash + int(rText[i])) % mod
+		patternHash = (base*patternHash + int64(rPattern[i])) % mod
+		textHash = (base*textHash + int64(rText[i])) % mod
 	}
 	for i := 0; i <= lenText-lenPattern; i++ {
 		if patternHash == textHash {
 			match := true
 			for j := range lenPattern {
 				if rText[i+j] != rPattern[j] {
 					match = false
 					break
 				}
 			}
 			if match {
 				res = append(res, i)
 			}
 		}
 		if i < lenText-lenPattern {
-			textHash = (base*(textHash-int(rText[i])*h) + int(rText[i+lenPattern])) % mod
+			textHash = (base*(textHash-int64(rText[i])*h) + int64(rText[i+lenPattern])) % mod
 			if textHash < 0 {
 				textHash += mod
 			}
 		}
 	}
 	return res
 }

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 51aa66d and ca556c3.

📒 Files selected for processing (1)
  • challenge-23/submissions/PopovMarko/solution-template.go

@RezaSi RezaSi merged commit c61b2f0 into RezaSi:main Mar 16, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants