Press "Enter" to skip to content

Posts published in “String”

花花酱 LeetCode 28. Implement strStr()

Problem

ImplementĀ strStr().

Return the index of the first occurrence of needle in haystack, orĀ -1Ā if needle is not part of haystack.

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1

Clarification:

What should we return whenĀ needleĀ is an empty string? This is a great question to ask during an interview.

For the purpose of this problem, we will return 0 whenĀ needleĀ is an empty string. This is consistent to C’sĀ strstr()Ā and Java’sĀ indexOf().

Solution 1: Brute Force

Time complexity: O(mn)

Space complexity: O(1)

C++

Python3

花花酱 LeetCode 916. Word Subsets

Problem

We are given two arraysĀ AĀ andĀ BĀ of words.Ā  Each word is a string of lowercase letters.

Now, say thatĀ wordĀ bĀ is a subset of wordĀ aĀ if every letter inĀ bĀ occurs inĀ a,Ā including multiplicity.Ā  For example,Ā "wrr"Ā is a subset ofĀ "warrior", but is not a subset ofĀ "world".

Now say a wordĀ aĀ fromĀ AĀ isĀ universalĀ if for everyĀ bĀ inĀ B,Ā bĀ is a subset ofĀ a.

Return a list of all universal words inĀ A.Ā  You can return the words in any order.

Example 1:

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["e","o"]
Output: ["facebook","google","leetcode"]

Example 2:

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["l","e"]
Output: ["apple","google","leetcode"]

Example 3:

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["e","oo"]
Output: ["facebook","google"]

Example 4:

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["lo","eo"]
Output: ["google","leetcode"]

Example 5:

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["ec","oc","ceo"]
Output: ["facebook","leetcode"]

Note:

  1. 1 <= A.length, B.length <= 10000
  2. 1 <= A[i].length, B[i].lengthĀ <= 10
  3. A[i]Ā andĀ B[i]Ā consist only of lowercase letters.
  4. All words inĀ A[i]Ā are unique: there isn’tĀ i != jĀ withĀ A[i] == A[j].

Solution: Hashtable

Find the max requirement for each letter from B.

Time complexity: O(|A|+|B|)

Space complexity: O(26)

C++

花花酱 LeetCode 14. Longest Common Prefix

Problem

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty stringĀ "".

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase lettersĀ a-z.

Solution: Brute Force

Time complexity: O(mk), where k the length of common prefix.

Space complexity: O(k)

C++

Java

Time complexity: (mk + k^2)

Python3

 

花花酱 LeetCode 13. Roman to Integer

Problem

Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58
Explanation: C = 100, L = 50, XXX = 30 and III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Solution

accumulate the value of each letter.

If the value of current letter is greater than the previous one, deduct twice of the previous value.

e.g. IX, 1 + 10 – 2 * 1 = 9 instead of 1 + 10 = 11

Time complexity: O(n)

Space complexity: O(1)

C++

Java

Python3

花花酱 LeetCode 10. Regular Expression Matching

Problem

Given an input string (s) and a pattern (p), implement regular expression matching with support forĀ '.'Ā andĀ '*'.

'.' Matches any single character.
'*' Matches zero or more of the preceding element.

The matching should cover theĀ entireĀ input string (not partial).

Note:

  • sĀ could be empty and contains only lowercase lettersĀ a-z.
  • pĀ could be empty and contains only lowercase lettersĀ a-z, and characters likeĀ .Ā orĀ *.

Example 1:

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

Input:
s = "aa"
p = "a*"
Output: true
Explanation:Ā '*' means zero or more of the precedengĀ element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".

Example 3:

Input:
s = "ab"
p = ".*"
Output: true
Explanation:Ā ".*" means "zero or more (*) of any character (.)".

Example 4:

Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation:Ā c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".

Example 5:

Input:
s = "mississippi"
p = "mis*is*p*."
Output: false

Solution 1: Recursion

Time complexity: O((|s| + |p|) * 2 ^Ā (|s| + |p|))

Space complexity: O(|s| + |p|)

C++