How to find repeating sequence of characters in a given array?

后端 未结 14 830
故里飘歌
故里飘歌 2020-12-02 12:42

My problem is to find the repeating sequence of characters in the given array. simply, to identify the pattern in which the characters are appearing.

          


        
14条回答
  •  臣服心动
    2020-12-02 13:16

    Not sure how you define "efficiently". For easy/fast implementation you could do this in Java:

        private static String findSequence(String text) {
            Pattern pattern = Pattern.compile("(.+?)\\1+");
            Matcher matcher = pattern.matcher(text);
            return matcher.matches() ? matcher.group(1) : null;
        }
    

    it tries to find the shortest string (.+?) that must be repeated at least once (\1+) to match the entire input text.

提交回复
热议问题