You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java
+6Lines changed: 6 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,12 @@ public static String longestPrefix(String[] str){
15
15
if(n==0){
16
16
return"";
17
17
}
18
+
/*
19
+
Sort the Array: Sort the array of strings to bring strings with common prefixes adjacent to each other.
20
+
Identify Extremes: Select the first and last strings from the sorted array for comparison, as they will have the longest common prefix.
21
+
Character Comparison: Compare the characters of the first and last strings until a mismatch is found, tracking the index of the last matching character.
22
+
Return Prefix: Return the substring of the first string from the start to the index of the last matching character, which represents the longest common prefix.
0 commit comments