MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / Abbreviation

Function Abbreviation

dynamic/abbreviation.go:26–47  ·  view source on GitHub ↗

Returns true if it is possible to make a equals b (if b is an abbreviation of a), returns false otherwise

(a string, b string)

Source from the content-addressed store, hash-verified

24
25// Returns true if it is possible to make a equals b (if b is an abbreviation of a), returns false otherwise
26func Abbreviation(a string, b string) bool {
27 dp := make([][]bool, len(a)+1)
28 for i := range dp {
29 dp[i] = make([]bool, len(b)+1)
30 }
31 dp[0][0] = true
32
33 for i := 0; i < len(a); i++ {
34 for j := 0; j <= len(b); j++ {
35 if dp[i][j] {
36 if j < len(b) && strings.ToUpper(string(a[i])) == string(b[j]) {
37 dp[i+1][j+1] = true
38 }
39 if string(a[i]) == strings.ToLower(string(a[i])) {
40 dp[i+1][j] = true
41 }
42 }
43 }
44 }
45
46 return dp[len(a)][len(b)]
47}

Callers 1

TestAbbreviationFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestAbbreviationFunction · 0.68