Word Break

中等 单词切分
11%
通过

给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词。

您在真实的面试中是否遇到过这个题? 
Yes
样例

给出

s = "lintcode"

dict = ["lint","code"]

返回 true 因为"lintcode"可以被空格切分成"lint code"

标签  Expand 

public class Solution {
    /**
     * @param s: A string s
     * @param dict: A dictionary of words dict
     */
    public boolean wordSegmentation(String s, Set<String> dict) {
       if (s == null || s.length() == 0) {
            return true;
        }
        //找到dict 中最长单词的长度
        int maxLength = getMaxLength(dict);
        //
        boolean[] canSegment = new boolean[s.length() + 1];
        
        //最开始设为true, 符合s.length()==0的情况
        canSegment[0] = true;
        //遍历s里的每个单词
        for (int i = 1; i <= s.length(); i++) {
            canSegment[i] = false;
            //
            for (int j = 1; j <= maxLength && j <= i; j++) {
                //canSegement[i-j] == false, continue ==> j+1, 开始下一轮循环
                if (!canSegment[i - j]) {
                    continue;
                }
                /**
                 * substring(int beginIndex,int endIndex)
                 * 返回一个新字符串,它是此字符串的一个子字符串。
                 * 该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。
                 * 因此,该子字符串的长度为 endIndex-beginIndex。
                 * "smiles".substring(1, 5) returns "mile"
                 * beginIndex - 起始索引(包括)。
                 * endIndex - 结束索引(不包括)。
                 */
                String word = s.substring(i - j, i);
                //如果dict包含word,canSegment[i]设为true,break
                if (dict.contains(word)) {
                    canSegment[i] = true;
                    break;
                }
            }
        }

        return canSegment[s.length()];
    }
    
    private int getMaxLength(Set<String> dict) {
        int maxLength = 0;
        for (String word : dict) {
            maxLength = Math.max(maxLength, word.length());
        }
        return maxLength;
    }
}


/**
         *    0 1 2 3 4 5 6 7 
         * s="l i n t c o d e" dict=["lint","code"]
         * s.length = 8, maxlength = 4;
         * i = 1, 
         *      canSegment[1] = false,
         *      j = 1, canSegment[0] = true,s.substring(0,1) ==> "l" ==> if;
         * i = 2,
         *      canSegment[2] = false,
         *      j = 1, canSegment[1] = false,continue;
         *      j = 2, canSegment[0] = true,s.substring(0,2) ==> "li" ==> if; 
         * i = 3,
         *      canSegment[3] = false,
         *      j = 1, canSegment[2] = false,continue;
         *      j = 2, canSegment[1] = false,continue;
         *      j = 3, canSegment[0] = true,s.substring(0,3) ==> "lin" ==> if; 
         *i = 4,
         *      canSegment[4] = false,
         *      j = 1, canSegment[3] = false,continue;
         *      j = 2, canSegment[2] = false,continue;
         *      j = 3, canSegment[1] = false,continue;
         *      j = 4, canSegment[0] = true, word = s.substring(0,4) ==> 
         *                    "lint" ==> if ==> canSegment[4] = true, break;
         * i = 5,
         *      canSegment[5] = false,
         *      j = 1, canSegment[4] = true,s.substring(4,5) ==>"c" ==> if;
         *      j = 2, canSegment[3] = false,continue;
         *      j = 3, canSegment[2] = false,continue;
         *      j = 4, canSegment[1] = false,continue;
         *      j = 5, canSegment[0] = true,s.substring(0,5) ==>"lintc" ==> if;
         *      
         * i = 6,
         *      canSegment[6] =false,
         *      j = 1, canSegment[5] = false,continue;
         *      j = 2, canSegment[4] = true,s.substring(4,6) ==>"co" ==> if;
         *      j = 3, canSegment[3] = false,continue;
         *      j = 4, canSegment[2] = false,continue;
         *      j = 5, canSegment[1] = false,continue;
         *      j = 6, canSegment[0] = true,s.substring(0,6) ==>"lintco" ==> if;
         * 
         * i = 7,
         *      canSegment[7] =false,
         *      j = 1, canSegment[6] = false,continue;
         *      j = 2, canSegment[5] = false,continue;
         *      j = 3, canSegment[4] = true,s.substring(4,7) ==>"cod" ==> if;
         *      j = 4, canSegment[3] = false,continue;
         *      j = 5, canSegment[2] = false,continue;
         *      j = 6, canSegment[1] = false,continue; 
         *      j = 7, canSegment[0] = true,s.substring(0,7) ==>"lintcod" ==> if; 
         * 
         * i = 8,
         *      canSegment[8] =false,
         *      j = 1, canSegment[7] = false,continue;
         *      j = 2, canSegment[6] = false,continue;
         *      j = 3, canSegment[5] = false,continue;
         *      j = 4, canSegment[4] = true,s.substring(4,8) ==>"code" ==> if
         *                                  ==> canSegment[8] = true, break;
         */



/**
         *    0 1 2 3 4 5 6 7 8 9 
         * s="l i n t c o d e l i" dict=["lint","code"]
         * s.length = 10, maxlength = 4;
         * i = 1, 
         *      canSegment[1] = false,
         *      j = 1, canSegment[0] = true,s.substring(0,1) ==> "l" ==> if;
         * i = 2,
         *      canSegment[2] = false,
         *      j = 1, canSegment[1] = false,continue;
         *      j = 2, canSegment[0] = true,s.substring(0,2) ==> "li" ==> if; 
         * i = 3,
         *      canSegment[3] = false,
         *      j = 1, canSegment[2] = false,continue;
         *      j = 2, canSegment[1] = false,continue;
         *      j = 3, canSegment[0] = true,s.substring(0,3) ==> "lin" ==> if; 
         *i = 4,
         *      canSegment[4] = false,
         *      j = 1, canSegment[3] = false,continue;
         *      j = 2, canSegment[2] = false,continue;
         *      j = 3, canSegment[1] = false,continue;
         *      j = 4, canSegment[0] = true, word = s.substring(0,4) ==> 
         *                    "lint" ==> if ==> canSegment[4] = true, break;
         * i = 5,
         *      canSegment[5] = false,
         *      j = 1, canSegment[4] = true,s.substring(4,5) ==>"c" ==> if;
         *      j = 2, canSegment[3] = false,continue;
         *      j = 3, canSegment[2] = false,continue;
         *      j = 4, canSegment[1] = false,continue;
         *      j = 5, canSegment[0] = true,s.substring(0,5) ==>"lintc" ==> if;
         *      
         * i = 6,
         *      canSegment[6] =false,
         *      j = 1, canSegment[5] = false,continue;
         *      j = 2, canSegment[4] = true,s.substring(4,6) ==>"co" ==> if;
         *      j = 3, canSegment[3] = false,continue;
         *      j = 4, canSegment[2] = false,continue;
         *      j = 5, canSegment[1] = false,continue;
         *      j = 6, canSegment[0] = true,s.substring(0,6) ==>"lintco" ==> if;
         * 
         * i = 7,
         *      canSegment[7] =false,
         *      j = 1, canSegment[6] = false,continue;
         *      j = 2, canSegment[5] = false,continue;
         *      j = 3, canSegment[4] = true,s.substring(4,7) ==>"cod" ==> if;
         *      j = 4, canSegment[3] = false,continue;
         *      j = 5, canSegment[2] = false,continue;
         *      j = 6, canSegment[1] = false,continue; 
         *      j = 7, canSegment[0] = true,s.substring(0,7) ==>"lintcod" ==> if; 
         * 
         * i = 8,
         *      canSegment[8] =false,
         *      j = 1, canSegment[7] = false,continue;
         *      j = 2, canSegment[6] = false,continue;
         *      j = 3, canSegment[5] = false,continue;
         *      j = 4, canSegment[4] = true,s.substring(4,8) ==>"code" ==> if
         *                                  ==> canSegment[8] = true, break;
         * i = 9,
         *      canSegment[9] =false,
         *      j = 1, canSegment[8] = true,s.substring(8,9) ==>"l" ==> if;
         *      j = 2, canSegment[7] = false,continue;
         *      j = 3, canSegment[6] = false,continue;
         *      j = 4, canSegment[5] = false,continue;
         *      j = 5, canSegment[4] = true,s.substring(4,9) ==>"codel" ==> if;
         *      j = 6, canSegment[3] = false,continue;
         *      j = 7, canSegment[2] = false,continue;
         *      j = 8, canSegment[1] = false,continue;
         *      j = 9, canSegment[0] = true,s.substring(0,9) ==>"lintcodel" ==> if;
         *
         * i = 10,
         *      canSegment[10] =false,
         *      j = 1, canSegment[9] = false,continue;
         *      j = 2, canSegment[8] = true,s.substring(8,10) ==>"li" ==> if;
         *      j = 3, canSegment[7] = false,continue;
         *      j = 4, canSegment[6] = false,continue;
         *      j = 5, canSegment[5] = false,continue;
         *      j = 6, canSegment[4] = true,s.substring(4,10) ==>"codeli" ==> if;
         *      j = 7, canSegment[3] = false,continue;
         *      j = 8, canSegment[2] = false,continue;
         *      j = 9, canSegment[1] = false,continue;
         *      j = 10, canSegment[0] = true,s.substring(0,10) ==>"lintcodeli" ==> if;
         *                                  
         */




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值