admin管理员组文章数量:1516870
拓维面试题找最长重复子串,哈哈
标题:最长重复子串
定义重复字符串是由两个相同的字符串首尾拼接而成,例如 便是长度为6的一个重复字符串,而 则不存在重复字符串。
给定一个字符串,请返回其最长重复子串的长度。
若不存在任何重复字符子串,则返回 0 。
本题中子串的定义是字符串中一段连续的区间。
数据范围:字符串长度不大于 ,保证字符串一定由小写字母构成。
进阶:空间复杂度 ,时间复杂度
备注:
示例1:
输入
ababc
输出
4
public class TuoWeiExamieDemo {private static String a = "abcabcaaabcdefgaabcdefg";public static void main(String[] args) {char[] arr = a.toCharArray();List<String> list = new ArrayList<>();int arrLength = arr.length;for (int i = 0; i < arrLength; i++) {for (int j = i + 1; j < arrLength; j++) {char chari = arr[i];char charj = arr[j];if (chari == charj) {int subLength = j - i;try {String subStr1 = a.substring(i, i + subLength);String subStr2 = a.substring(j, j + subLength);if (subStr1.equals(subStr2)) {list.add(subStr1 + subStr2);}} catch (StringIndexOutOfBoundsException e) {break;}}}}int maxLength = 0;String maxLengthStr = "";for (String string : list) {int length = string.length();if (length > maxLength) {maxLength = length;maxLengthStr = string;}}System.out.println(list);System.out.println(maxLengthStr);System.out.println(maxLength);}
}本文标签: 拓维面试题找最长重复子串哈哈
版权声明:本文标题:拓维面试题找最长重复子串,哈哈 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.betaflare.com/biancheng/1710119168a761791.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论