Skip to content

Instantly share code, notes, and snippets.

@wszdwp
Created November 12, 2014 03:15
Show Gist options
  • Select an option

  • Save wszdwp/7edf3adaf2c00ecf502a to your computer and use it in GitHub Desktop.

Select an option

Save wszdwp/7edf3adaf2c00ecf502a to your computer and use it in GitHub Desktop.
Write a function to find the longest common prefix string amongst an array of strings.
public String longestCommonPrefix(String[] strs) {
int n = strs.length;
if (n == 0)
return "";
if (n == 1)
return strs[0];
String prefix = strs[0];
for (int i = 1; i < n; i++) {
int j = 0;
for (; j < prefix.length(); j++) {
if (j >= prefix.length() || j >= strs[i].length() || strs[i].charAt(j) != prefix.charAt(j)) {
break;
}
}
prefix = prefix.substring(0, j);
}
return prefix;
}
@SubhamKhinchi

Copy link
Copy Markdown

it is showing error " no member named CharAt"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment