Created
November 12, 2014 03:15
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it is showing error " no member named CharAt"