Last active
September 5, 2017 07:22
-
-
Save lazygyu/bf6dfee69b1739bc675aecd555d59955 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(strs, t) | |
cache = {} | |
que = [] | |
que.push([t, 0]) | |
depth = 0 | |
strs.uniq! | |
return 1 if strs.include?(t) | |
cur = 0 | |
while que.size > 0 and cur < que.size do | |
tar, dep = que[cur] | |
cur += 1 | |
dep += 1 | |
strs.each do |str| | |
return dep if tar == str | |
next if str.size > tar.size | |
next if not tar.start_with?(str) | |
target = tar[str.size, tar.size - 1] | |
if !cache[target] | |
cache[target] = 1 | |
que.push([target, dep]) | |
end | |
end | |
end | |
-1 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @param strs given strings | |
// @param cache cache | |
// @param targetStr left string which has to be solved | |
// @return the minimum number of strings used | |
private int solution(String[] strs, Map<String, Integer> cache, String targetStr) { | |
if (cache.containsKey(targetStr)) | |
return cache.get(targetStr); | |
int min = Integer.MAX_VALUE; | |
for (String str : strs) { | |
if (targetStr.equals(str)) { // end of string | |
cache.put(targetStr, 1); | |
return 1; | |
} | |
else if (targetStr.startsWith(str)) { // matched so far | |
int left = solution(strs, cache, targetStr.substring(str.length())); | |
if (left == -1) { // no solutions. Skip. | |
continue; | |
} | |
min = Math.min(min, left + 1); | |
} | |
} | |
if (min == Integer.MAX_VALUE) { // no solution found | |
cache.put(targetStr, -1); | |
return -1; | |
} else { | |
cache.put(targetStr, min); | |
return min; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkString(strs, t){ | |
var que = []; | |
var cache = {}; | |
strs = strs.filter(el=>t.indexOf(el)>-1); | |
que.push([t,0]); | |
while(que.length > 0){ | |
var tar = que.shift(); | |
var tStr = tar[0]; | |
var dep = tar[1]; | |
dep++; | |
if( dep > t.length ) return -1; | |
for(var i=0,str;str=strs[i++];){ | |
if(str.length > tStr.length || !tStr.startsWith(str) ) continue; | |
if( tStr === str ) return dep; | |
var item = [tStr.substr(str.length), dep]; | |
if( !cache[item[0]] ){ | |
cache[item[0]] = dep; | |
que.push(item); | |
} | |
} | |
}; | |
return -1; | |
} | |
function solution(strs, t) { | |
return checkString(strs, t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment