当前位置:网站首页>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
边栏推荐
- MySQL information schema learning (II) -- InnoDB table
- 算法面试经典100题,Android程序员最新职业规划
- Learn to explore - use pseudo elements to clear the high collapse caused by floating elements
- 在解决了 2961 个用户反馈后,我做出了这样的改变...
- IC设计流程中需要使用到的文件
- The list of people who passed the fifth phase of personal ability certification assessment was published
- 社招面试心得,2022最新Android高频精选面试题分享
- A popular explanation will help you get started
- 史上超级详细,想找工作的你还不看这份资料就晚了
- Using clip path to draw irregular graphics
猜你喜欢
中缀表达式转后缀表达式详细思路及代码实现
RT-Thread 组件 FinSH 使用时遇到的问题
Interpretation of Dagan paper
学习探索-无缝轮播图
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
Php+redis realizes the function of canceling orders over time
PMP practice once a day | don't get lost in the exam -7.6
PMP每日一练 | 考试不迷路-7.6
CCNP Part 11 BGP (III) (essence)
LeetCode-1279. 红绿灯路口
随机推荐
In depth analysis, Android interview real problem analysis is popular all over the network
保证接口数据安全的10种方案
【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...
LeetCode_ Gray code_ Medium_ 89. Gray code
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
LeetCode_双指针_中等_61. 旋转链表
Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference
How to access localhost:8000 by mobile phone
学习探索-函数防抖
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
从sparse.csc.csr_matrix生成邻接矩阵
算法面试经典100题,Android程序员最新职业规划
Learning and Exploration - function anti shake
Don't miss this underestimated movie because of controversy!
Lick the dog until the last one has nothing (simple DP)
map的使用(列表的数据赋值到表单,json逗号隔开显示赋值)
冒烟测试怎么做
黑馬--Redis篇
Interpretation of Dagan paper
Problems encountered in using RT thread component fish