Write a function to find the longest common common string amongst an array of strings.
来源:互联网 发布:java生成jar文件 编辑:程序博客网 时间:2024/04/29 20:06
这个算法我是求的公共子序列,而不是公共前缀。提交到公共前缀的online不会通过。
public class Solution { public String longestCommonPrefix(String[] strs) { // Note: The Solution object is instantiated only once and is reused by each test case. if(strs==null||strs.length==0){ return ""; } if(strs.length==1){ return strs[0]; } String s=strs[0]; for (int i=1;i<strs.length;i++ ){ s=findTwo(s,strs[i]); } return s; } String findTwo(String s0,String s1){ if(s0==null||s1==null||s0==""||s1==""){ return ""; } if(s0.charAt(0)==(s1.charAt(0))){ if (s0.length()==1||s1.length()==1){ return s0.substring(0,1); } return s0.charAt(0)+findTwo(s0.substring(1),s1.substring(1)); } else { if(s0.length()==1||s1.length()==1){ if(s0.length()==s1.length()){ return ""; } else return s0.length()>s1.length()? findTwo(s1,s0.substring(1)):findTwo(s0,s1.substring(1)); } String ss0=findTwo(s0,s1.substring(1)); String ss1=findTwo(s0.substring(1),s1); int temp0=(ss0=="")?0:ss0.length(); int temp1=(ss1=="")?0:ss1.length(); return temp0>temp1?ss0:ss1 ; } } public static void main(String[] args){ Solution test=new Solution(); String[] strs={"afg","dafegee"}; System.out.println(test.longestCommonPrefix(strs)); } }
- Write a function to find the longest common common string amongst an array of strings.
- Write a function to find the longest common prefix string amongst an array of strings.
- 4、Write a function to find the longest common prefix string amongst an array of strings
- Find common characters in a set of strings
- Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ
- Write a function to find the depth of a binary tree
- Write a function to find the depth of a binary tree
- Given two strings s and t, write a function to determine if t is an anagram of s.
- Given two strings s and t, write a function to determine if t is an anagram of s.
- Write a C program to find the parity of an unsigned integer
- stackover flow 上的一个问题:Find the most common entry in an array
- search the longest common substr in 2 strings
- How can I combine elements of an array that have a common element?
- Given an integer, write a function to determine if it is a power of two.
- Given an integer, write a function to determine if it is a power of two
- a common misunderstanding of string.Trim functions
- Find the maxest element of an array
- 634. Find the Derangement of An Array
- volatile语义及线程安全singleton模式探讨
- 强迫自己学习(实践篇),以及关于写博客的几点建议
- 百度笔试总结
- WF4.0 基础篇 (十四) Delay 与WF4中的线程
- Android下QQ空间查看大图特效
- Write a function to find the longest common common string amongst an array of strings.
- windows下用VS2010 build ceres solver 库
- android listview item定义不同的样式布局
- 十亿数据中求最大一百个数字
- 2014华为校园招聘机考(10.12北京)
- mongodb_分布式存储_切片
- if语句中判断两值是否相等的小技巧
- C# 将2进制转化为单精度的方法
- 强迫自己学习(心态篇),国庆,你准备去哪疯?