fork download
  1. public class Main { // Теперь имя совпадает с ожидаемым Main.java
  2. public static String longestCommonPrefix(String[] strs) {
  3. if (strs == null || strs.length == 0) return "";
  4.  
  5. for (int i = 0; i < strs[0].length(); i++) {
  6. char c = strs[0].charAt(i);
  7. for (int j = 1; j < strs.length; j++) {
  8. if (i == strs[j].length() || strs[j].charAt(i) != c) {
  9. return strs[0].substring(0, i);
  10. }
  11. }
  12. }
  13. return strs[0];
  14. }
  15.  
  16. public static void main(String[] args) {
  17. String[] data = {"префикс", "преграда", "прекрасный"};
  18. System.out.println(longestCommonPrefix(data));
  19. }
  20. }
  21.  
Success #stdin #stdout 0.1s 54732KB
stdin
Standard input is empty
stdout
пре