BruteForceStringMatching2

前端之家收集整理的这篇文章主要介绍了BruteForceStringMatching2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//The brute-force algorithm is to solve the problem of //counting,in a given text,the number of substrings that starts //with an A and ends with a B.
//For example,there are four such substrings in CABAAXBYA.

package main

import (
    "fmt"
)

func StringMatching2(text []rune) int {
    n := len(text)
    count := 0

    for i := 0; i <= n-2; i++ {

        for j := 0; i+j < n; {

            if j == 0 {
                if text[i+j] == 'A' {
                    j++
                    continue
                } else {
                    break
                }

            } else {
                if text[i+j] == 'B' {
                    count++
                }
                j++
            }
        }
    }

    return count
}

func main() {

    text := []rune("CABBBBB")

    fmt.Println(StringMatching2(text))
}
原文链接:https://www.f2er.com/go/190451.html

猜你在找的Go相关文章