Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use wildcards in leetcode

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use wildcards in leetcode, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Given a string (s) and a character pattern (p), implement a support for'?' Matches the wildcard character of'*'.

'?' You can match any single character.

'*' can match any string (including an empty string).

The match is considered successful only if the two strings match exactly.

Description:

S may be empty and contain only lowercase letters from amurz.

P may be empty and contain only lowercase letters from Amurz, as well as characters? And *.

Example 1:

Enter:

S = "aa"

P = "a"

Output: false

Explanation: "a" does not match the entire string "aa".

Example 2:

Enter:

S = "aa"

P = "*"

Output: true

Explanation:'* 'can match any string.

Example 3:

Enter:

S = "cb"

P = "? a"

Output: false

Explanation:'?' Can match 'croup', but the second'a 'cannot match' b'.

Example 4:

Enter:

S = "adceb"

P = "* aqb"

Output: true

Explanation: the first'* 'can match the empty string and the second' * 'can match the string' dce'.

Example 5:

Enter:

S = "acdcb"

P = "a*c?b"

Input: false

Ideas for solving the problem:

1, wildcards and regular expressions are two things, regular expressions are used for string matching, wildcards are used for shell programming

2. The idea of solving the problem is similar, except that the boundary condition is different from the state transfer equation.

3. State transfer equation:

If p [j] = * {

/ / match 1 or 0

A [I] [j] = a [I] [jmur1] | | a [I-1] [j]

} else {

A [I] [j] = a [I-1] [jmur1] & & (s [I] = = s [j] | | s [j] = ='?')

}

Since * may match 0, considering that p has the beginning of *, the size of the array is len (s) + 1 Perlin (p) + 1.

Func isMatch (s string, p string) bool {if len (s) = = 0 & & len (p) = = 0 {return true} if len (p) = = 0 {return false} if len (s) = 0 {for I: = 0; I < len (p) Bs + {if [] byte (p) [I]! ='*'{return false}} return true} bs: = [] byte (s) bp: = [] byte (p) a: = make ([] [] bool, len (s) + 1) for I: = 0; I < len (s) + 1 ITunes + {a [I] = make ([] bool, len (p) + 1)} a [0] [0] = true / / a [1len I] [0] = false / / a [0] [1len j] = a [0] [jmer1] & & b [j] = ='* 'for j: = 1; j < len (p) + 1; jink + {a [0] [j] = a [0] [JLV 1] & & BP [j-1] = =' *'}

For I: = 1; I < len (s) + 1; iTunes + {for j: = 1; j < len (p) + 1 Match + {if bp [j-1] ='*'{/ / 01a [I] [j] = a [I] [jmur1] | | a [I-1] [j]} else {a [I] [j] = a [I-1] [JLV 1] & & match (bs [I-1], bp [j-1])}} return Alen (s)] [len (p)]

}

Func match (a, b byte) bool {return a = = b | | b = ='?'} these are all the contents of the article "how to use wildcards in leetcode". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report