请设计程序:输入一个字符串,程序会输出不含有重复字符的最长子串的长度。
1个回答
展开全部
//刚才写的。。。。import java.io.DataInputStream;import java.io.IOException; public class StringLength { public String search(String s1, String s2) { String max = ""; for (int i = 0; i < s1.length(); i++) { for (int j = i; j < s1.length(); j++) { String sub = s1.substring(i, j); if ((s2.indexOf(sub) != -1) && sub.length() > max.length()) { max = sub; } } } return max; } public static void main(String[] args) { String a = ""; String b = ""; System.out.println("请输入第一个字符串:"); a = inputString.getString(); System.out.println("请输入第二个字符串:"); b = inputString.getString(); String output = new StringLength().search(a, b); System.out.println("相同的字符串是:" + output + "长度为:" + output.length()); }} class inputString { public static String getString() { DataInputStream dis = new DataInputStream(System.in); String value = null; try { @SuppressWarnings("deprecation") String str = dis.readLine(); value = str; } catch (IOException e) { e.printStackTrace(); } return value; }}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询