简体   繁体   English

将字符串分成两个或三个单词的组

[英]Splitting the string in group of two or three words

given a string, we have to split the string in all distinct two and three parts (permutation doesn't matter).给定一个字符串,我们必须将字符串拆分为所有不同的两部分和三部分(排列无关紧要)。 for example for:例如:

cat

answer is答案是

ca t
ct a
c at
c a t

for为了

aaa

answer is答案是

aa a
a a a

how to do that?怎么做? can you please provide a code in the best possible time complexity?你能提供一个尽可能好的时间复杂度的代码吗?

So i wrote a code for your problem, I wrote this code especially for string containing 3 characters.所以我为你的问题写了一个代码,我专门为包含 3 个字符的字符串编写了这段代码。 This code is nothing just "System.out.println" written in different style, if this doesn't suit you please provide me more input i would like it to solve then.` public void checkString(String s) { String strArry[] = s.split("");这段代码只是用不同风格编写的“System.out.println”,如果这不适合你,请提供更多输入,我希望它能够解决。` public void checkString(String s) { String strArry[] = s.split("");

    if (strArry[0].equalsIgnoreCase(strArry[1])) {
        System.out.println(strArry[0] + strArry[1] + " " + strArry[2]);
        System.out.println(strArry[0] + " " + strArry[1] + " " + strArry[2]);

    } else {
        {
            System.out.println(strArry[0] + strArry[1] + " " + strArry[2]);
            System.out.println(strArry[0] + strArry[2] + " " + strArry[1]);
            System.out.println(strArry[0] + " " + strArry[1] + strArry[2]);
            System.out.println(strArry[0] + " " + strArry[1] + " " + strArry[2]);
        }
    }
}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM