java 找出所有加起来等于给定字符串的子字符串的组合

tct7dpnv  于 2023-02-11  发布在  Java
关注(0)|答案(7)|浏览(142)

我正在尝试创建一个数据结构来保存所有可能的子字符串组合,这些组合加起来就是原始字符串。例如,如果字符串是"java",则有效结果将是"j", "ava""ja", "v", "a",无效结果将是"ja", "a""a", "jav"
我很容易就找到了所有可能的子串

String string = "java";
    List<String> substrings = new ArrayList<>();
    for( int c = 0 ; c < string.length() ; c++ )
    {
        for( int i = 1 ; i <= string.length() - c ; i++ )
        {
            String sub = string.substring(c, c+i);
            substrings.add(sub);
        }
    }
    System.out.println(substrings);

现在我试着构造一个只包含有效子字符串的结构。但是这并不容易。我陷入了一段非常丑陋的代码的迷雾中,摆弄着索引,没有接近完成的地方,很可能是完全走错了路。有什么提示吗?

h4cxqtbf

h4cxqtbf1#

这里有一个方法:

static List<List<String>> substrings(String input) {

    // Base case: There's only one way to split up a single character
    // string, and that is ["x"] where x is the character.
    if (input.length() == 1)
        return Collections.singletonList(Collections.singletonList(input));

    // To hold the result
    List<List<String>> result = new ArrayList<>();

    // Recurse (since you tagged the question with recursion ;)
    for (List<String> subresult : substrings(input.substring(1))) {

        // Case: Don't split
        List<String> l2 = new ArrayList<>(subresult);
        l2.set(0, input.charAt(0) + l2.get(0));
        result.add(l2);

        // Case: Split
        List<String> l = new ArrayList<>(subresult);
        l.add(0, input.substring(0, 1));
        result.add(l);
    }

    return result;
}

输出:

[java]
[j, ava]
[ja, va]
[j, a, va]
[jav, a]
[j, av, a]
[ja, v, a]
[j, a, v, a]
q9rjltbz

q9rjltbz2#

看起来这是一个问题,即找到字符串长度的compositions,然后用这些组合来生成子串,所以一个数n有2^n-1个组合,这对于长字符串来说可能有点耗时......

nfs0ujit

nfs0ujit3#

也许有人会喜欢另一种非递归的解决方案,并且不占用内存来保存列表:

public static List<List<String>> substrings(final String input) {
    if(input.isEmpty())
        return Collections.emptyList();
    final int size = 1 << (input.length()-1); 
    return new AbstractList<List<String>>() {

        @Override
        public List<String> get(int index) {
            List<String> entry = new ArrayList<>();
            int last = 0;
            while(true) {
                int next = Integer.numberOfTrailingZeros(index >> last)+last+1;
                if(next == last+33)
                    break;
                entry.add(input.substring(last, next));
                last = next;
            }
            entry.add(input.substring(last));
            return entry;
        }

        @Override
        public int size() {
            return size;
        } 
    };
}

public static void main(String[] args) {
    System.out.println(substrings("java"));
}

输出:

[[java], [j, ava], [ja, va], [j, a, va], [jav, a], [j, av, a], [ja, v, a], [j, a, v, a]]

它只是根据索引计算下一个组合。

yb3bgrhw

yb3bgrhw4#

为了防止有人在Python中寻找相同的算法,下面是Python中的实现:

from itertools import combinations

def compositions(s):
    n = len(s)
    for k in range(n):
        for c in combinations(range(1, n), k):
            yield tuple(s[i:j] for i, j in zip((0,) + c, c + (n,)))

示例如何工作:

>>> for x in compositions('abcd'):
...     print(x)
('abcd',)
('a', 'bcd')
('ab', 'cd')
('abc', 'd')
('a', 'b', 'cd')
('a', 'bc', 'd')
('ab', 'c', 'd')
('a', 'b', 'c', 'd')

只需稍加修改,就可以按不同顺序生成合成:

def compositions(s):
    n = len(s)
    for k in range(n):
        for c in itertools.combinations(range(n - 1, 0, -1), k):
            yield tuple(s[i:j] for i, j in zip((0,) + c[::-1], c[::-1] + (n,)))

它会给予你:

>>> for x in compositions('abcd'):
...     print(x)
('abcd',)
('abc', 'd')
('ab', 'cd')
('a', 'bcd')
('ab', 'c', 'd')
('a', 'bc', 'd')
('a', 'b', 'cd')
('a', 'b', 'c', 'd')

再加上一个小的附加功能,您可以只生成指定数量的拆分:

def compositions(s, r=None):
    n = len(s)
    r = range(n) if r is None else [r - 1]
    for k in r:
        for c in itertools.combinations(range(n - 1, 0, -1), k):
            yield tuple(s[i:j] for i, j in zip((0,) + c[::-1], c[::-1] + (n,)))
>>> for x in compositions('abcd', 3):
...     print(x)
('ab', 'c', 'd')
('a', 'bc', 'd')
('a', 'b', 'cd')
mpbci0fu

mpbci0fu5#

另一种递归解决方案只是将结果添加到列表中

static List<List<String>> substrings(String input) {
    List<List<String>> result = new ArrayList<>();
    if (input.length() == 1) {
        result.add(Arrays.asList(new String[]{input}));
    }
    else {
        //iterate j, ja, jav, jav
        for (int i = 0; i < input.length()-1; i++ ) {
            String root = input.substring(0,i+1);
            String leaf = input.substring(i+1);
            for( List<String> strings: substrings(leaf) ) {
                ArrayList<String> current = new ArrayList<String>();
                current.add(root);
                current.addAll(strings);
                result.add(current);
            }
        }
        //adds the whole string as one of the leaves (ie. java, ava, va, a)
        result.add(Arrays.asList(new String[]{input}));
    }
    return result;
}
k5ifujac

k5ifujac6#

这个问题可以通过这个代码来解决。

public static List<String> subsets(String s) {
if(Objects.isNull(s) || s.length() ==0){
    return Collections.emptyList();
}
int length = s.length();
List<String> result = new ArrayList<>();

for (int i = 0; i < length; i++) { // Group loop
    String substring = "";
    for (int j = 0; j < length; j++) { //
        if (i + j > length - 1) {
            substring = s.substring(j) + s.substring(0, ((i + j) - length) + 1);
        } else {
            substring = s.substring(j, j + i + 1);
        }
        result.add(substring);
    }
}
return result;}
    • 输出**

[a、b、c、d、ab、bc、cd、da、abc、bcd、cda、dab、abcd、bcda、cdab、dabc]

nzkunb0c

nzkunb0c7#

你可以用下面的公式来计算。

print(2**(len("ABCD")-1))

这里我使用ABCD作为我的输入字符串。

相关问题