当前位置:网站首页>Leetcode 30. Concatenate substrings of all words
Leetcode 30. Concatenate substrings of all words
2022-07-06 19:37:00 【Java full stack R & D Alliance】
The code is as follows :
public static List<Integer> findSubstring(String s, String[] words) {
List<Integer> result = new ArrayList<>();
int length = 0;
for (String word : words) {
length += word.length();
}
for (int i = 0; i <= s.length() - length; i++) {
String substring = s.substring(i, i + length);
boolean tran = tran(substring, words);
if (tran) {
result.add(i);
}
}
return result;
}
public static boolean tran(String str, String[] words) {
// Dealing with border situations
if (words.length == 1) {
return str.equals(words[0]);
}
boolean result = false;
for (int i = 0; i < words.length; i++) {
if (str.startsWith(words[i])) {
StringBuilder stringBuilder = new StringBuilder(str);
String substring = stringBuilder.delete(0, words[i].length()).toString();
String[] restWords = subWords(words, i);
if (tran(substring, restWords)) {
result = true;
}
}
}
return result;
}
public static String[] subWords(String[] words, int i) {
String[] strings = Arrays.copyOf(words, words.length);
for (int k = i; k < strings.length - 1; k++) {
strings[k] = strings[k + 1];
}
strings = Arrays.copyOfRange(strings, 0, strings.length - 1);
return strings;
}
But there is still some abnormal input , Cause code timeout . It's shameless. This kind of input 
边栏推荐
- LeetCode-1279. Traffic light intersection
- 三面蚂蚁金服成功拿到offer,Android开发社招面试经验
- 如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
- The second day of rhcsa study
- 中缀表达式转后缀表达式详细思路及代码实现
- Hudi vs Delta vs Iceberg
- 企业精益管理体系介绍
- Zero foundation entry polardb-x: build a highly available system and link the big data screen
- 黑马--Redis篇
- Lick the dog until the last one has nothing (simple DP)
猜你喜欢

PMP每日一练 | 考试不迷路-7.6

Computer network: sorting out common network interview questions (I)

Analysis of rainwater connection

Druid database connection pool details

PMP practice once a day | don't get lost in the exam -7.6

zabbix 代理服务器 与 zabbix-snmp 监控

Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)

Black Horse - - Redis Chapter

安装Mysql报错:Could not create or access the registry key needed for the...

谷粒商城--分布式高级篇P129~P339(完结)
随机推荐
In 50W, what have I done right?
Simple application of VBA script in Excel
史上超级详细,想找工作的你还不看这份资料就晚了
Translation D28 (with AC code POJ 26:the nearest number)
[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
打家劫舍III[后序遍历与回溯+动态规划]
Vmware虚拟机无法打开内核设备“\\.\Global\vmx86“的解决方法
【翻译】Linkerd在欧洲和北美的采用率超过了Istio,2021年增长118%。
Spark foundation -scala
Characteristic colleges and universities, jointly build Netease Industrial College
Learning and Exploration - Seamless rotation map
Elastic search indexes are often deleted [closed] - elastic search indexes gets deleted frequently [closed]
MySQL information schema learning (I) -- general table
Synchronous development of business and application: strategic suggestions for application modernization
[translation] Digital insider. Selection process of kubecon + cloudnativecon in Europe in 2022
PMP practice once a day | don't get lost in the exam -7.6
[pytorch] yolov5 train your own data set
MySQL information Schema Learning (i) - - General table
算法面试经典100题,Android程序员最新职业规划
Benefit a lot, Android interview questions