import java.util.*;
/**
* Created with IntelliJ IDEA.
* User: uerturk
* Date: 08/10/13
* Time: 17:04
* To change this template use File | Settings | File Templates.
*/
class HasChain {
Map
<Character, List
<String
>> startsWithStringMap
= new HashMap
<Character, List
<String
>>();
return str.charAt(0);
}
return str.charAt(str.length() - 1);
}
boolean hasChain(List<String> stringList) {
for (String str
: stringList
) { List<String> startsWithList;
List<String> endsWithList;
if (startsWithStringMap.containsKey(start)) {
startsWithList = startsWithStringMap.get(start);
} else {
startsWithList = new ArrayList<String>();
startsWithStringMap.put(start, startsWithList);
}
if (endsWithStringMap.containsKey(end)) {
endsWithList = endsWithStringMap.get(end);
} else {
endsWithList = new ArrayList<String>();
endsWithStringMap.put(end, endsWithList);
}
startsWithList.add(str);
endsWithList.add(str);
}
Stack<String> stringStack = new Stack<String>();
for (String str
: stringList
) { if (hasChain(stringList.size(), str, stringStack)) {
System.
out.
println(stringStack
);
return true;
}
}
return false;
}
private boolean hasChain
(int size,
String startString, Stack
<String
> stringStack
) { if (size == stringStack.size()) return true;
if (startsWithStringMap.containsKey(last)) {
List<String> stringList = startsWithStringMap.get(last);
for (int i = 0; i < stringList.size(); i++) {
String candidate
= stringList.
remove(i
--); stringStack.push(candidate);
if (hasChain(size, candidate, stringStack)) {
return true;
}
stringStack.pop();
stringList.add(++i, candidate);
}
}
return false;
}
public static void main
(String[] args
) { List<String> stringList = new ArrayList<String>();
stringList.add("bd");
stringList.add("fk");
stringList.add("ab");
stringList.add("kl");
stringList.add("cf");
stringList.add("ff");
stringList.add("fa");
stringList.add("ak");
stringList.add("ka");
stringList.add("lf");
stringList.add("bc");
System.
out.
println(new HasChain
().
hasChain(stringList
));
stringList.remove("ka");
System.
out.
println(new HasChain
().
hasChain(stringList
));
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKLyoqCiAqIENyZWF0ZWQgd2l0aCBJbnRlbGxpSiBJREVBLgogKiBVc2VyOiB1ZXJ0dXJrCiAqIERhdGU6IDA4LzEwLzEzCiAqIFR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2l0aExpc3QgPSBzdGFydHNXaXRoU3RyaW5nTWFwLmdldChzdGFydCk7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBzdGFydHNXaXRoTGlzdCA9IG5ldyBBcnJheUxpc3Q8U3RyaW5nPigpOwogICAgICAgICAgICAgICAgc3RhcnRzV2l0aFN0cmluZ01hcC5wdXQoc3RhcnQsIHN0YXJ0c1dpdGhMaXN0KTsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgaWYgKGVuZHNXaXRoU3RyaW5nTWFwLmNvbnRhaW5zS2V5KGVuZCkpIHsKICAgICAgICAgICAgICAgIGVuZHNXaXRoTGlzdCA9IGVuZHNXaXRoU3RyaW5nTWFwLmdldChlbmQpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgZW5kc1dpdGhMaXN0ID0gbmV3IEFycmF5TGlzdDxTdHJpbmc+KCk7CiAgICAgICAgICAgICAgICBlbmRzV2l0aFN0cmluZ01hcC5wdXQoZW5kLCBlbmRzV2l0aExpc3QpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHN0YXJ0c1dpdGhMaXN0LmFkZChzdHIpOwogICAgICAgICAgICBlbmRzV2l0aExpc3QuYWRkKHN0cik7CiAgICAgICAgfQoKICAgICAgICBTdGFjazxTdHJpbmc+IHN0cmluZ1N0YWNrID0gbmV3IFN0YWNrPFN0cmluZz4oKTsKICAgICAgICBmb3IgKFN0cmluZyBzdHIgOiBzdHJpbmdMaXN0KSB7CiAgICAgICAgICAgIGlmIChoYXNDaGFpbihzdHJpbmdMaXN0LnNpemUoKSwgc3RyLCBzdHJpbmdTdGFjaykpIHsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihzdHJpbmdTdGFjayk7CgogICAgICAgICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIHJldHVybiBmYWxzZTsKICAgIH0KCiAgICBwcml2YXRlIGJvb2xlYW4gaGFzQ2hhaW4oaW50IHNpemUsIFN0cmluZyBzdGFydFN0cmluZywgU3RhY2s8U3RyaW5nPiBzdHJpbmdTdGFjaykgewogICAgICAgIGlmIChzaXplID09IHN0cmluZ1N0YWNrLnNpemUoKSkgcmV0dXJuIHRydWU7CiAgICAgICAgQ2hhcmFjdGVyIGxhc3QgPSBnZXRMYXN0Q2hhcihzdGFydFN0cmluZyk7CiAgICAgICAgaWYgKHN0YXJ0c1dpdGhTdHJpbmdNYXAuY29udGFpbnNLZXkobGFzdCkpIHsKICAgICAgICAgICAgTGlzdDxTdHJpbmc+IHN0cmluZ0xpc3QgPSBzdGFydHNXaXRoU3RyaW5nTWFwLmdldChsYXN0KTsKICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBzdHJpbmdMaXN0LnNpemUoKTsgaSsrKSB7CiAgICAgICAgICAgICAgICBTdHJpbmcgY2FuZGlkYXRlID0gc3RyaW5nTGlzdC5yZW1vdmUoaS0tKTsKICAgICAgICAgICAgICAgIHN0cmluZ1N0YWNrLnB1c2goY2FuZGlkYXRlKTsKICAgICAgICAgICAgICAgIGlmIChoYXNDaGFpbihzaXplLCBjYW5kaWRhdGUsIHN0cmluZ1N0YWNrKSkgewogICAgICAgICAgICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RyaW5nU3RhY2sucG9wKCk7CiAgICAgICAgICAgICAgICBzdHJpbmdMaXN0LmFkZCgrK2ksIGNhbmRpZGF0ZSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIHJldHVybiBmYWxzZTsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgTGlzdDxTdHJpbmc+IHN0cmluZ0xpc3QgPSBuZXcgQXJyYXlMaXN0PFN0cmluZz4oKTsKICAgICAgICBzdHJpbmdMaXN0LmFkZCgiYmQiKTsKICAgICAgICBzdHJpbmdMaXN0LmFkZCgiZmsiKTsKICAgICAgICBzdHJpbmdMaXN0LmFkZCgiYWIiKTsKICAgICAgICBzdHJpbmdMaXN0LmFkZCgia2wiKTsKICAgICAgICBzdHJpbmdMaXN0LmFkZCgiY2YiKTsKICAgICAgICBzdHJpbmdMaXN0LmFkZCgiZmYiKTsKICAgICAgICBzdHJpbmdMaXN0LmFkZCgiZmEiKTsKICAgICAgICBzdHJpbmdMaXN0LmFkZCgiYWsiKTsKICAgICAgICBzdHJpbmdMaXN0LmFkZCgia2EiKTsKICAgICAgICBzdHJpbmdMaXN0LmFkZCgibGYiKTsKICAgICAgICBzdHJpbmdMaXN0LmFkZCgiYmMiKTsKCiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKG5ldyBIYXNDaGFpbigpLmhhc0NoYWluKHN0cmluZ0xpc3QpKTsKCiAgICAgICAgc3RyaW5nTGlzdC5yZW1vdmUoImthIik7CgogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihuZXcgSGFzQ2hhaW4oKS5oYXNDaGFpbihzdHJpbmdMaXN0KSk7CgogICAgfQp9Cg==
[bc, cf, fk, kl, lf, ff, fa, ak, ka, ab, bd]
true
false