Press "Enter" to skip to content

Posts published in “String”

花花酱 LeetCode 3517. Smallest Palindromic Rearrangement I

这题考查的是排序吧… 还有rbegin的使用。

普通排序:时间 O(nlogn) / 空间 O(1)

前半部分顺序排序,后半部分逆序排序。

计数排序:时间:O(n),空间:O(n) -> O(1)

首尾一起写

花花酱 LeetCode 2023. Number of Pairs of Strings With Concatenation Equal to Target

方法1: Brute Force

枚举所有的(nums[i], nums[j])组合,相加在和target比较。

时间复杂度:O(mn2) m为字符串的最长长度。
空间复杂度:O(m)

优化前 67ms, 49.3M

一些工程上的优化

  • 用string_view作为参数类型,减少一次string copy
  • 先比较长度,再判断内容。
  • string_view.substr 是O(1)时间,和直接用strncmp比较内容是一样的。

优化后 3ms, 12.88MB

花花酱 LeetCode 2296 Design a Text Editor

方法1:双向链表来存储文本,迭代器指向光标所在的位置。加一个dummy head会使代码简单很多。

时间复杂度:TextEditor O(1) addText O(|text|) deleteText O(k) cursorLeft O(k) cursorRight(k)
空间复杂度:O(n)

由于每个字符需要创建一个节点(17+个字节),虽然没有超时,但实际工程中是不能使用这种方式的。

方法2: 用两个string来代表光标左边的字符串和光标右边的字符串。注:右边字符串是反转的。

左右两边的字符串只会增长(或者覆盖),不会缩短,这是用空间换时间。

删除的时候就是修改了长度而已,并没有缩短字符串,所以是O(1)的。
如果缩短的话需则要O(k)时间,还要加上内存释放/再分配的时间,应该会慢一些但不多。

移动光标的时候,就是把左边字符串的最后k个copy到右边的最后面,或者反过来。同样没有缩短,只会变长。

时间复杂度: TextEditor O(1) addText O(|text|) deleteText O(1) cursorLeft O(k) cursorRight(k)
空间复杂度:O(n),n为构建的最长的字符串

花花酱 LeetCode 3498 Reverse Degree of a String

送分题,简单仿真即可。

时间复杂度:O(n)
空间复杂度:O(1)

花花酱 LeetCode 3029. Minimum Time to Revert Word to Initial State I

You are given a 0-indexed string word and an integer k.

At every second, you must perform the following operations:

  • Remove the first k characters of word.
  • Add any k characters to the end of word.

Note that you do not necessarily need to add the same characters that you removed. However, you must perform both operations at every second.

Return the minimum time greater than zero required for word to revert to its initial state.

Example 1:

Input: word = "abacaba", k = 3
Output: 2
Explanation: At the 1st second, we remove characters "aba" from the prefix of word, and add characters "bac" to the end of word. Thus, word becomes equal to "cababac".
At the 2nd second, we remove characters "cab" from the prefix of word, and add "aba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state.
It can be shown that 2 seconds is the minimum time greater than zero required for word to revert to its initial state.

Example 2:

Input: word = "abacaba", k = 4
Output: 1
Explanation: At the 1st second, we remove characters "abac" from the prefix of word, and add characters "caba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state.
It can be shown that 1 second is the minimum time greater than zero required for word to revert to its initial state.

Example 3:

Input: word = "abcbabcd", k = 2
Output: 4
Explanation: At every second, we will remove the first 2 characters of word, and add the same characters to the end of word.
After 4 seconds, word becomes equal to "abcbabcd" and reverts to its initial state.
It can be shown that 4 seconds is the minimum time greater than zero required for word to revert to its initial state.

Constraints:

  • 1 <= word.length <= 50
  • 1 <= k <= word.length
  • word consists only of lowercase English letters.

Solution: Suffix ==? Prefix

Compare the suffix with prefix.

word = “abacaba”, k = 3
ans = 1, “abacaba” != “abacaba
ans = 2, “abacaba” == “abacaba“, we find it.

Time complexity: O(n * n / k)
Space complexity: O(1)

C++