如何检查整数中的重复序列 [英] How to check for repeating sequence in an integer

查看:116
本文介绍了如何检查整数中的重复序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字母数字字符串,我想检查它中的模式重复只是为了整数。它们应该是连续的。

I have an alpha-numeric string and I want to check for pattern repetition in it just for the integers. And they should be continuous.

示例


  1. 12341234qwe 应该告诉我重复 1234

  2. 1234qwe1234 告诉我 1234 重复,因为它不连续。

  3. 12121212 应该被视为 12 被重复,因为这是第一个被发现重复的集合。但是如果有一个算法会在 12 之前找到 1212 作为重复集合,那么我猜它必须在 1212 上再次执行这些步骤。

  1. 12341234qwe should tell me 1234 is repeated.
  2. 1234qwe1234 should NOT tell me that 1234 is repeated since its not continuous.
  3. 12121212 should be treated as 12 being repeated as that is the first set which would be found being repeated. But if there is an algorithm which would find 1212 as the repeated set before 12 then I guess it has to perform the steps again on 1212.

我认为我可以通过迭代并将其与(< =')进行比较来存储整数部分0'&&> ='9')在另一个 StringBuilder 中。然后我读到关于在字符串上执行FFT并显示重复模式。但是我不知道如何在Java中执行FFT并查找结果,我也希望在不进行信号处理的情况下尝试这样做。我读到了KMP模式匹配,但只适用于给定的输入。有没有其他方法可以做到这一点?

What I thought was I can store the integer part by iterating and comparing it with ( <= '0' && >= '9') in a different StringBuilder. Then I read about performing FFT on the string and it shows the repeated patterns. But I have no idea on how to perform FFT in Java and look for the results, also, I was hoping to try to do this without going to Signal Processing. I read about KMP pattern matching but that only works with a given input. Is there any other way to do this?

推荐答案

你可以借助正则表达式来解决这个问题。考虑这样的代码:

You can take help of regex to solve this I think. Consider code like this:

String arr[] = {"12341234abc", "1234foo1234", "12121212", "111111111", "1a1212b123123c12341234d1234512345"};
String regex = "(\\d+?)\\1";
Pattern p = Pattern.compile(regex);
for (String elem : arr) {
    boolean noMatchFound = true;
    Matcher matcher = p.matcher(elem);
    while (matcher.find()) {
        noMatchFound = false;
        System.out.println(elem + " got repeated: " + matcher.group(1));
    }
    if (noMatchFound) {
        System.out.println(elem + " has no repeation");
    }
}

输出:

abc12341234abc got repeated: 1234
1234foo1234 has no repeation
12121212 got repeated: 12
12121212 got repeated: 12
111111111 got repeated: 1
111111111 got repeated: 1
111111111 got repeated: 1
111111111 got repeated: 1
1a1212b123123c12341234d1234512345 got repeated: 12
1a1212b123123c12341234d1234512345 got repeated: 123
1a1212b123123c12341234d1234512345 got repeated: 1234
1a1212b123123c12341234d1234512345 got repeated: 12345



说明:



正在使用的正则表达式是(\\d +?)\\1 其中

\\d        - means a numerical digit
\\d+       - means 1 or more occurrences of a digit
\\d+?      - means reluctant (non-greedy) match of 1 OR more digits
( and )    - to group the above regex into group # 1
\\1        - means back reference to group # 1
(\\d+?)\\1 - repeat the group # 1 immediately after group # 1

这篇关于如何检查整数中的重复序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆