日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

LeetCode 1087. Brace Expansion

 印度阿三17 2019-11-04

原題鏈接在這里:https:///problems/brace-expansion/

題目:

A string?S?represents a list of words.

Each letter in the word has 1 or more options.? If there is one option, the letter is represented as is.? If there is more than one option, then curly braces delimit the options.? For example,?"{a,b,c}"?represents options?["a", "b", "c"].

For example,?"{a,b,c}d{e,f}"?represents the list?["ade", "adf", "bde", "bdf", "cde", "cdf"].

Return all words that can be formed in this manner, in lexicographical order.

Example 1:

Input: "{a,b}c{d,e}f"
Output: ["acdf","acef","bcdf","bcef"]

Example 2:

Input: "abcd"
Output: ["abcd"]

Note:

  1. 1 <= S.length <= 50
  2. There are no?nested curly brackets.
  3. All characters inside a pair of?consecutive opening and ending curly brackets are different.

題解:

If there is curly braces, all the chars in it could be candidate.

Starting from index 0. Do DFS, DFS state needs orginal string, current index, current StringBuilder and res collection.

If current index i points to '{', then find next index j points to '}', for each of candidate inside brances, append it to StringBuilder and continue DFS at index j 1. After DFS, do backtracking.

If current index i points to char, append it to StringBuilder and continue DFS at index i 1. After DFS, do bracktracking.

When current index points to the end of string, add copy of StringBuilder to res collection.

Time Complexity: exponential.

Space: O(n). n = S.length(). stack space.

AC Java:

 1 class Solution {
 2     public String[] expand(String S) {
 3         if(S == null || S.length() == 0){
 4             return new String[0];
 5         }
 6         
 7         List<String> res = new ArrayList<String>();
 8         dfs(S, 0, new StringBuilder(), res);
 9         Collections.sort(res);
10         return res.toArray(new String[0]);
11     }
12     
13     private void dfs(String s, int i, StringBuilder sb, List<String> res){
14         if(i >= s.length()){
15             res.add(sb.toString());
16             return;
17         }
18         
19         
20         if(s.charAt(i) == '{'){
21             int j = i 1;
22             while(j<s.length() && s.charAt(j)!='}'){
23                 j  ;
24             }
25             
26             String [] candidates = s.substring(i 1, j).split(",");
27             for(String candidate : candidates){
28                 sb.append(candidate);
29                 dfs(s, j 1, sb, res);
30                 sb.deleteCharAt(sb.length()-1);
31             }
32         }else{
33             sb.append(s.charAt(i));
34             dfs(s, i 1, sb, res);
35             sb.deleteCharAt(sb.length()-1);
36         }
37     }
38 }

?

來源:https://www./content-4-546501.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約