CareerCup String getSentence(String text, Set<String> dictionary); O(n)
来源:互联网 发布:怪物猎人ol捏脸数据图 编辑:程序博客网 时间:2024/05/22 00:13
String getSentence(String text, Set<String> dictionary);
// text is a string without spaces, you need to insert spaces into text, so each word seperated by the space in the resulting string exists in the dictionary, return the resulting string
// running time has to be at least as good as O(n)
// text is a string without spaces, you need to insert spaces into text, so each word seperated by the space in the resulting string exists in the dictionary, return the resulting string
// running time has to be at least as good as O(n)
// getSentence("iamastudentfromwaterloo", {"from, "waterloo", "hi", "am", "yes", "i", "a", "student"}) -> "i am a student from waterloo"
---------------------------------------------------------------
import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Set;class Solution {public static void main(String[] args) {Solution instance = new Solution();Set<String> dictionary = new HashSet();dictionary.add("google");dictionary.add("is");dictionary.add("awesome");List<String> store = new ArrayList<String>();instance.printWords("googleisawesome", store, dictionary);for(int i = store.size() - 1; i >= 0; --i) {System.out.println(store.get(i));}}private boolean printWords(String string, List<String> store, Set<String> dictionary) {if(string.length() == 0) {return true;}for(int i = 1; i <= string.length(); ++i) {String curWord = string.substring(0, i);if(dictionary.contains(curWord) && printWords(string.substring(i), store, dictionary)) {store.add(curWord);return true;}}return false;}}
0 0
- CareerCup String getSentence(String text, Set<String> dictionary); O(n)
- Dictionary<string, string>
- OC - Array,Dictionary,set,string方法总结
- swift练习-String、Array、Dictionary、Set
- CareerCup Given a dictionary, how would you add spaces in this string
- 字典 Dictionary<string, double>
- Swift中 String、Array、Set、Dictionary的遍历方法
- 泛型Dictionary<string,string>的用法
- 遍历Hashtable、IDictionary、Dictionary<string, string>
- Dictionary<string, string>是一个泛型
- C# 不分大小写的 Dictionary<string, string>
- 遍历Hashtable、IDictionary、Dictionary<string, string>
- 泛型Dictionary<string,string>的用法
- Dictionary<string, List<string>> 的使用
- Dictionary 和 List<Dictionary<string, object>>
- 160630 string 与dictionary互转
- string
- String
- 【第一章】 Spring概述 ——跟我学Spring3 收藏
- JAVAh
- action,servlet单实例线程安全问题
- Makefile编写基本规则
- 手动脱壳VMP
- CareerCup String getSentence(String text, Set<String> dictionary); O(n)
- VMP 1.7 主程序破解方法
- 计算多项式的值
- 关于==与equals()区别
- HDU2016
- 【Java核心技术——反射
- JAVA关键字总结
- 深刻理解Linux进程间通信(IPC)
- Android anotations试用