Add solution for Challenge 17 by atplay90#1900
Conversation
WalkthroughAdds a Go command-line palindrome checker with input cleaning, case normalization, two-pointer comparison, and exported ChangesPalindrome feature
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.2)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" 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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
challenge-17/submissions/atplay90/solution-template.go (1)
23-47: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low valueConsider an in-place two-pointer approach to optimize space complexity.
The current approach allocates new strings when filtering out non-alphanumeric characters and converting to lowercase, resulting in O(N) space complexity. You can optimize this to O(1) space complexity by using a two-pointer approach that evaluates characters from both ends in-place.
(Note: Applying this refactor will make the
"strings"import unused, so remember to remove it to prevent a compilation error.)♻️ Proposed refactor for O(1) space complexity
-func removeNonAlphanumeric(s string) string { - var result strings.Builder - for _, r := range s { - if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') { - result.WriteRune(r) - } - } - return result.String() -} - // IsPalindrome checks if a string is a palindrome. // A palindrome reads the same backward as forward, ignoring case, spaces, and punctuation. func IsPalindrome(s string) bool { - - // 1. Clean the string (remove spaces, punctuation, and convert to lowercase) - s = removeNonAlphanumeric(s) - s = strings.ToLower(s) - // 2. Check if the cleaned string is the same forwards and backwards - for i := 0; i < len(s)/2; i++ { - if s[i] != s[len(s)-1-i] { - return false - } - } - return true + for i, j := 0, len(s)-1; i < j; { + if !isAlphanumeric(s[i]) { + i++ + continue + } + if !isAlphanumeric(s[j]) { + j-- + continue + } + if toLower(s[i]) != toLower(s[j]) { + return false + } + i++ + j-- + } + return true +} + +func isAlphanumeric(c byte) bool { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') +} + +func toLower(c byte) byte { + if c >= 'A' && c <= 'Z' { + return c + 'a' - 'A' + } + return c }
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 3b2c9261-fe36-4cb1-93e9-0d67cbdd39be
📒 Files selected for processing (1)
challenge-17/submissions/atplay90/solution-template.go
Challenge 17 Solution
Submitted by: @atplay90
Challenge: Challenge 17
Description
This PR contains my solution for Challenge 17.
Changes
challenge-17/submissions/atplay90/solution-template.goTesting
Thank you for reviewing my submission! 🚀