首页 > 其他分享 >[LeetCode] 1668. Maximum Repeating Substring

[LeetCode] 1668. Maximum Repeating Substring

时间:2022-11-03 15:14:31浏览次数:75  
标签:substring word repeating sequence int ababc Substring Repeating LeetCode

For a string sequence, a string word is k-repeating if word concatenated k times is a substring of sequence. The word's maximum k-repeating value is the highest value k where word is k-repeating in sequence. If word is not a substring of sequenceword's maximum k-repeating value is 0.

Given strings sequence and word, return the maximum k-repeating value of word in sequence.

Example 1:

Input: sequence = "ababc", word = "ab"
Output: 2
Explanation: "abab" is a substring in "ababc".

Example 2:

Input: sequence = "ababc", word = "ba"
Output: 1
Explanation: "ba" is a substring in "ababc". "baba" is not a substring in "ababc".

Example 3:

Input: sequence = "ababc", word = "ac"
Output: 0
Explanation: "ac" is not a substring in "ababc". 

Constraints:

  • 1 <= sequence.length <= 100
  • 1 <= word.length <= 100
  • sequence and word contains only lowercase English letters.

最大重复子字符串。

给你一个字符串 sequence ,如果字符串 word 连续重复 k 次形成的字符串是 sequence 的一个子字符串,那么单词 word 的 重复值为 k 。单词 word 的 最大重复值 是单词 word 在 sequence 中最大的重复值。如果 word 不是 sequence 的子串,那么重复值 k 为 0 。

给你一个字符串 sequence 和 word ,请你返回 最大重复值 k 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/maximum-repeating-substring
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意不难理解,问 word 最多可以重复多少次,依然是 sequence 的一个子串。word 的重复可以用 StringBuilder 进行拼接,检查子串可以用Java自带的 String.contains() 函数,但是注意时间复杂度会到 O(n^2)。

时间O(n^2)

空间O(n) - StringBuilder

Java实现

 1 class Solution {
 2     public int maxRepeating(String sequence, String word) {
 3         // corner case
 4         if (sequence.equals(word)) {
 5             return 1;
 6         }
 7 
 8         // normal case
 9         int m = sequence.length();
10         int n = word.length();
11         int max = m / n;
12         for (int k = max; k >= 1; k--) {
13             StringBuilder sb = new StringBuilder();
14             for (int i = 0; i < k; i++) {
15                 sb.append(word);
16             }
17             String sub = sb.toString();
18             if (sequence.contains(sub)) {
19                 return k;
20             }
21         }
22         return 0;
23     }
24 }

 

讨论区看到一个更简洁的代码。复杂度相同。

 1 class Solution {
 2     public int maxRepeating(String sequence, String word) {
 3         int count = 0;
 4         StringBuilder sb = new StringBuilder(word);
 5         while (sequence.contains(sb)) {
 6             count++;
 7             sb.append(word);
 8         }
 9         return count;
10     }
11 }

 

LeetCode 题目总结

标签:substring,word,repeating,sequence,int,ababc,Substring,Repeating,LeetCode
From: https://www.cnblogs.com/cnoodle/p/16854494.html

相关文章

  • leetcode - 94. 二叉树的中序遍历
    94.二叉树的中序遍历List<Integer>inorder=newArrayList<>();publicList<Integer>inorderTraversal(TreeNoderoot){wit......
  • [数据库基础]-- 字符串截取函数substr、substring以及 case when函数使用
    使用说明:1、使用:substr使用范围:oracle、mysql、sqlserversubstring使用范围:mysql、sqlserver 2、举例:现有表:t_user name、age字段查询需求:如果name字段中的第5个字符有“......
  • leetcode257-二叉树的所有路径
    257.二叉树的所有路径 泪目,自己写出的递归遍历./***Definitionforabinarytreenode.*structTreeNode{*intval;*TreeNode*left;*......
  • leetcode88
    合并两个有序数组Category Difficulty Likes Dislikesalgorithms Easy(52.02%) 1623 -TagsCompanies给你两个按非递减顺序排列的整数数组nums1和nums2,另有两个......
  • leetcode - 617. 合并二叉树
    617.合并二叉树classSolution{//迭代publicTreeNodemergeTreesWithStack(TreeNoderoot1,TreeNoderoot2){//如果当前root左右子树有一个是空......
  • LeetCode221. 最大正方形-中等(Go版)
    前情提示Go语言学习者,为了便于阅读和整理,本文代码已开源放在:https://github.com/honlu/GoDailyCodehttps://gitee.com/dreamzll/GoDailyCode持续更新中,已经完成排序算......
  • Leetcode第1668题:最大重复子字符串(Maximum repeating subarray)
    解题思路题目条件字符串长度不超过100,直接从大到小枚举word的重复次数k,判断word重复次数后是否还是sequence的字串,是就直接返回重复次数k。核心代码如下:classSolution......
  • leetcode-1880-easy
    CheckifWordEqualsSummationofTwoWordsThelettervalueofaletterisitspositioninthealphabetstartingfrom0(i.e.'a'->0,'b'->1,'c'->2,et......
  • leetcode-566-easy
    ReshapetheMatrixInMATLAB,thereisahandyfunctioncalledreshapewhichcanreshapeanmxnmatrixintoanewonewithadifferentsizerxckeepingits......
  • LeetCode刷题记录.Day4
    移除链表元素题目链接203.移除链表元素-力扣(LeetCode)classSolution{public:ListNode*removeElements(ListNode*head,intval){ListNode*varHe......