当前位置:网站首页>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 
边栏推荐
- 学习探索-使用伪元素清除浮动元素造成的高度坍塌
- MATLAB中deg2rad和rad2deg函数的使用
- RT-Thread 组件 FinSH 使用时遇到的问题
- 【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程
- About image reading and processing, etc
- CPU负载很低,loadavg很高处理方法
- A popular explanation will help you get started
- 力扣101题:对称二叉树
- 如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
- Druid database connection pool details
猜你喜欢

安装Mysql报错:Could not create or access the registry key needed for the...
![[translation] a GPU approach to particle physics](/img/07/57036c925155cab36678c696e89440.jpg)
[translation] a GPU approach to particle physics

C language daily practice - day 22: Zero foundation learning dynamic planning

黑馬--Redis篇

The list of people who passed the fifth phase of personal ability certification assessment was published

Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
![打家劫舍III[后序遍历与回溯+动态规划]](/img/9b/e9eeed138e46afdeed340bf2629ee1.png)
打家劫舍III[后序遍历与回溯+动态规划]

JDBC详解

Druid 数据库连接池 详解

凤凰架构3——事务处理
随机推荐
English topic assignment (25)
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
时钟轮在 RPC 中的应用
通俗的讲解,带你入门协程
Benefit a lot, Android interview questions
Translation D28 (with AC code POJ 26:the nearest number)
How to type multiple spaces when editing CSDN articles
Mysql Information Schema 學習(一)--通用錶
数学知识——高斯消元(初等行变换解方程组)代码实现
[translation] supply chain security project in toto moved to CNCF incubator
Druid 数据库连接池 详解
[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
Use of deg2rad and rad2deg functions in MATLAB
In 50W, what have I done right?
Application of clock wheel in RPC
Reflection and illegalaccessexception exception during application
MySql必知必会学习
Yyds dry goods inventory leetcode question set 751 - 760
How can my Haskell program or library find its version number- How can my Haskell program or library find its version number?
Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go