Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Jan 8, 2025
1 parent a7e4819 commit a90f6bc
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions leetcode/daily/3042/sol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/

package main

import "fmt"

func countPrefixSuffixPairs(words []string) int {
count := 0
for i := 0; i < len(words); i++ {
for j := i + 1; j < len(words); j++ {
if isPrefixAndSuffix(words[i], words[j]) {
count++
}
}
}

return count
}

func isPrefixAndSuffix(prefixSuffix, word string) bool {
n := len(prefixSuffix)
m := len(word)

prefixMatch := m >= n && word[:n] == prefixSuffix
suffixMatch := m >= n && word[m-n:] == prefixSuffix

return prefixMatch && suffixMatch
}

func main() {
words := []string{"a", "aba", "ababa", "aa"}
fmt.Println(countPrefixSuffixPairs(words))
}

0 comments on commit a90f6bc

Please sign in to comment.