String

10. Regular Expression Matching

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).

"""
先想只有 . 的情形, either s[i] == (p[i] || .)
再想 '*' 的兩種情況
1. repeat * 之前的char
2. ignore * 之前的char

Corner case
s: aa, a, ''
p: a* <- all valid

s為空, p 可不為空

"""

class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        if not p:
            return not s
        
        first_match = s and p[0] in (s[0], '.')
        
        if len(p) >=2 and p[1] == '*':
            return self.isMatch(s, p[2:]) or \
            (first_match and self.isMatch(s[1:], p))
            
        else:
            return first_match and self.isMatch(s[1:], p[1:])

44. Wildcard Matching

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

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

43. Multiply Strings

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

205 Isomorphic Strings

Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

884. Decoded String at Index

An encoded string S is given. To find and write the decoded string to a tape, the encoded string is read one character at a time and the following steps are taken:

If the character read is a letter, that letter is written onto the tape. If the character read is a digit (say d), the entire current tape is repeatedly written d-1 more times in total. Now for some encoded string S, and an index K, find and return the K-th letter (1 indexed) in the decoded string.

Example 1:

Input: S = "leet2code3", K = 10 Output: "o"

The decoded string is guaranteed to have less than 2^63 letters

  • Reduce K by K % word.len

  • Count the total string size, use long

  • if K == 0 && isLetter(char), return

  • Corner case: "ab2", 4

438. Find All Anagrams in a String

Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.

Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.

567 Permutation String

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.

  • Put s1 and s2 into 2 maps

  • A helper function to validate permutation

  • Loop to update the map

  • Time O(l1l2), Space O(226)

316. Remove Duplicate Letters

Given a string which contains only lowercase letters, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.

  • Greedy + stack

  • Only add char if it hasn't seen

  • Discard the last char in stack if

    1. The current char is smaller

    2. We can seen it later in string

Last updated

Was this helpful?