Press "Enter" to skip to content

Posts published in “String”

花花酱 LeetCode 567. Permutation in String

Problem

题ē›®å¤§ę„ļ¼šē»™ä½ s1, s2ļ¼Œé—®ä½ s2ēš„子äø²äø­ę˜Æ否存åœØs1ēš„äø€äøŖęŽ’åˆ—ć€‚

https://leetcode.com/problems/permutation-in-string/description/

Given two stringsĀ s1Ā andĀ s2, write a function to return true ifĀ s2Ā contains the permutation ofĀ s1. In other words, one of the first string’s permutations is theĀ substringĀ of the second string.

Example 1:

Input:s1 = "ab" s2 = "eidbaooo"
Output:True
Explanation: s2 contains one permutation of s1 ("ba").

Example 2:

Input:s1= "ab" s2 = "eidboaoo"
Output: False

Note:

  1. The input strings only contain lower case letters.
  2. The length of both given strings is in range [1, 10,000].

Solution: Sliding Window

Time Complexity: O(l1 + l2 * 26) = O(l1 + l2)

Space Complexity: O(26 * 2) = O(1)

C++

Related Problems

花花酱 LeetCode 228. Summary Ranges

Problem

Given a sorted integer array without duplicates, return the summary of its ranges.

Example 1:

Input:  [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: 0,1,2 form a continuous range;Ā 4,5 form a continuous range.

Example 2:

Input:  [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: 2,3,4 form a continuous range;Ā 8,9 form a continuous range.

Solution

Time complexity: O(n)

Space complexity: O(k)

C++

 

花花酱 LeetCode 856. Score of Parentheses

Problem

Given a balanced parentheses stringĀ S, compute the score of the string based on the following rule:

  • ()Ā has score 1
  • ABĀ has scoreĀ A + B, where A and B are balanced parentheses strings.
  • (A)Ā has scoreĀ 2 * A, where A is a balanced parentheses string.

 

Example 1:

Input: "()"
Output: 1

Example 2:

Input: "(())"
Output: 2

Example 3:

Input: "()()"
Output: 2

Example 4:

Input: "(()(()))"
Output: 6

Solution1: Recursion

Time complexity: O(n^2)

Space complexity: O(n)

 

Solution2: Counting

Time complexity: O(n)

Space complexity: O(1)

C++

 

花čƝ酱 LeetCode 859. Buddy Strings

Problem

Given two stringsĀ AĀ andĀ BĀ of lowercase letters, returnĀ trueĀ if and only if weĀ can swap two letters inĀ AĀ so that the result equalsĀ B.

 

Example 1:

Input: A = "ab", B = "ba"
Output: true

Example 2:

Input: A = "ab", B = "ab"
Output: false

Example 3:

Input: A = "aa", B = "aa"
Output: true

Example 4:

Input: A = "aaaaaaabc", B = "aaaaaaacb"
Output: true

Example 5:

Input: A = "", B = "aa"
Output: false

Note:

  1. 0 <= A.length <= 20000
  2. 0 <= B.length <= 20000
  3. AĀ andĀ BĀ consist only of lowercase letters.

Solution: HashTable

Time complexity: O(n)

Space complexity: O(26)

 

花花酱 LeetCode 848. Shifting Letters

Problem

We have a stringĀ SĀ of lowercase letters, and an integer arrayĀ shifts.

Call theĀ shiftĀ of a letter, the next letter in the alphabet, (wrapping around so thatĀ 'z'Ā becomesĀ 'a').

For example,Ā shift('a') = 'b',Ā shift('t') = 'u', andĀ shift('z') = 'a'.

Now for eachĀ shifts[i] = x, we want to shift the firstĀ i+1Ā letters ofĀ S,Ā xĀ times.

Return the final stringĀ after all such shifts toĀ SĀ are applied.

Example 1:

Input: S = "abc", shifts = [3,5,9]
Output: "rpl"
Explanation: 
We start with "abc".
After shifting the first 1 letters of S by 3, we have "dbc".
After shifting the first 2 letters of S by 5, we have "igc".
After shifting the first 3 letters of S by 9, we have "rpl", the answer.

Note:

  1. 1 <= S.length = shifts.length <= 20000
  2. 0 <= shifts[i] <= 10 ^ 9

Solution

Time complexity: O(n)

Space complexity: O(1)

C++