字符串列表的组合

时间:2015-12-30 13:02:07

标签: java math puzzle

以下是我需要在某些条件下使用的字符串列表。

" MSD" ," EEE"," RSR"," OCL"," SMS"," RTS"

组合的条件是

  1. 组合应该至少有两个字符串 (例如:(" EEE"
    ," RSR"),(" EEE"," RSR"," OCL"))
  2. 组合应该由相邻的字符串组成(例如:(" OCL"," SMS"),(" MSD"," EEE" ," RSR")是有效的。但不是(" EEE"," OCL")。因为" EEE"和" OCL& #34; 不是彼此相邻)
  3. Java实现非常受欢迎。

    SELECT
      REGEXP_REPLACE(phone_number,
                     '([[:digit:]]*)([[:digit:]]{3})',
                     '\1.\2') "REGEXP_REPLACE"
      FROM employees;
    

    对于我尝试过的代码的最新更新感到抱歉

1 个答案:

答案 0 :(得分:2)

for (int j = 0; j < str.length; j++) {
    String temp = "";
    for (int i = j; i < str.length; i++) {
        if ("".equals(temp))
            temp = str[i]; // assign the String to temp, but do not add to list yet
        else {
            temp = temp + " " + str[i];
            list.add(temp); // now that temp consists of at least two elements
                            // add them to the list
        }
    }
}

修复了列出单个条目的问题。从而导致:

MSD EEE
MSD EEE RSR
MSD EEE RSR OCL
MSD EEE RSR OCL SMS
MSD EEE RSR OCL SMS RTS
EEE RSR
EEE RSR OCL
EEE RSR OCL SMS
EEE RSR OCL SMS RTS
RSR OCL
RSR OCL SMS
RSR OCL SMS RTS
OCL SMS
OCL SMS RTS
SMS RTS