-
Notifications
You must be signed in to change notification settings - Fork 50
Add an implementation of finding the longest common prefix of an array of strings #130
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
base: main
Are you sure you want to change the base?
Conversation
…y of strings. Print out empty string if there is no common prefix. I aim this to be counted towards the Hacktoberfest.
Reviewer's GuideIntroduces a standalone Java class that computes the longest common prefix of command-line arguments by handling edge cases, iteratively reducing the prefix across all inputs, and using a helper to compare two strings. Class diagram for LongestCommonPrefix implementationclassDiagram
class LongestCommonPrefix {
+main(String[] args)
-getCommonPrefix(String a, String b) String
}
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
WalkthroughA new Java program class is introduced that computes the longest common prefix from command-line arguments. It processes multiple strings iteratively by comparing them character by character, returning the shared prefix or an empty line if none exists. Changes
Sequence Diagram(s)sequenceDiagram
participant Main
participant Args
participant Logic
participant Output
Main->>Args: Read command-line arguments
rect rgb(200, 220, 255)
Note over Logic: Process arguments
alt No arguments
Logic->>Output: Print empty line
else Single argument
Logic->>Output: Print argument
else Multiple arguments
Logic->>Logic: Initialize prefix = first argument
loop For each remaining argument
Logic->>Logic: Compute common prefix with current
alt Prefix becomes empty
Logic->>Output: Print empty line and exit
else Continue
Logic->>Logic: Update prefix
end
end
Logic->>Output: Print final prefix
end
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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 |
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Code Suggestions ✨Explore these optional code suggestions:
|
|||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (2)
Java/LargestCommonPrefix.java (2)
8-11: Simplify empty output.Use
System.out.println()instead ofSystem.out.println("")for clearer intent.Apply this diff:
if (args.length == 0) { - System.out.println(""); + System.out.println(); return; }
17-27: Algorithm implementation is sound.The iterative approach correctly computes the longest common prefix, and the early exit when the prefix becomes empty is a good optimization.
Consider applying the same simplification to line 22 as suggested for line 9:
if (prev.isEmpty()) { - System.out.println(""); + System.out.println(); return; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
Java/LargestCommonPrefix.java(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Sourcery review
🔇 Additional comments (1)
Java/LargestCommonPrefix.java (1)
30-40: Helper method is well-implemented.The character-by-character comparison correctly handles strings of different lengths and stops at the first mismatch. The logic is clear and correct.
| * Problem : To find the longest common prefix string amongst an array of strings, input on command line. | ||
| * Explanation : Sample command -- java LongestCommonPrefix apple application appointment | ||
| */ | ||
| class LongestCommonPrefix { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Filename and class name mismatch.
The class is named LongestCommonPrefix but the file is named LargestCommonPrefix.java. In Java, the public class name must exactly match the filename, otherwise the code will not compile.
Rename the file to match the class name:
-Java/LargestCommonPrefix.java
+Java/LongestCommonPrefix.javaCommittable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In Java/LargestCommonPrefix.java around line 5, the declared class name
LongestCommonPrefix does not match the filename LargestCommonPrefix.java; update
one to match the other to fix the compilation error — either rename the file to
LongestCommonPrefix.java or rename the class to public class LargestCommonPrefix
(and update any references/imports accordingly) so the public class name exactly
matches the filename.
Print out empty string if there is no common prefix. I aim this to be counted towards the Hacktoberfest. Thank you!
Summary by Sourcery
Implement a Java command-line utility that finds and prints the longest common prefix among given string arguments, defaulting to an empty string when there are no inputs or no shared prefix.
New Features:
Summary by CodeRabbit