如何计算字符串中连续重复字符的数量

huangapple go评论80阅读模式
英文:

How to count the number of consecutively repeated characters in a string

问题

基本上,我想要实现的是不允许有大量连续重复的字符/数字的密码。
我正在尝试编写一个"go"函数,我会向该函数传递一个字符串和一个允许的最大连续重复字符数,然后它应该告诉我是否超过了这个限制。

在以前的"javascript"中,我可以很容易地使用正则表达式来实现,方法如下:

var regexString = '(.)\{' + (maxAllowedRepetitions) + ',}';
var regex = new RegExp(regexString);
return regex.test(string)

...其中maxAllowedRepetitions是最大限制。如果限制是3,字符串是'blablabla',它返回false。如果字符串是'blablaaaabla',它返回true,因为字符'a'重复了超过3次。

我发现使用"go"的正则表达式实现同样的功能很困难。
我不介意不使用正则表达式,我只需要一个好的方法来实现这个功能。
有什么建议吗?

英文:

Basically, what I want to achieve is to not allow passwords with lots of consecutively repeated chars/digits.
I'm trying to write a <strong>go</strong> function to which I pass a string and a maximum allowed number of consecutively repeated characters, and it should tell me if it's surpassed or not.

I used to do it easily in <strong>javascript</strong> using the a regular expression in the following way:

var regexString = &#39;(.)\{&#39; + (maxAllowedRepetitions) + &#39;,}&#39;;
var regex = new RegExp(regexString);
return regex.test(string)

... where maxAllowedRepetitions is the max limit. If the limit is 3 and a string is 'blablabla', it returns false. If it is blablaaaabla, it returns true since the character 'a' is repeated more than 3 times.

I'm finding it difficult to achieve the same thing using <strong>go</strong> regexp.
I don't mind not using regexp. I just need a good way to achieve this.
Any suggestions?

答案1

得分: 2

package main

import "fmt"

func main() {

	// 重复的无效密码
	password := "blablaaaabla"
	fmt.Printf("%s 是否无效?%+v\n", password, invalid(password))

	// 没有重复的正确密码
	password = "blabla"
	fmt.Printf("%s 是否无效?%+v\n", password, invalid(password))

	// 含有Unicode字符的有效密码
	password = "bla∞∞bla"
	fmt.Printf("%s 是否无效?%+v\n", password, invalid(password))

	// 含有Unicode字符的无效密码
	password = "bla∞∞∞bla"
	fmt.Printf("%s 是否无效?%+v\n", password, invalid(password))
}

func invalid(s string) bool {
	var lastChar rune
	var lastCharCount = 0
	for _, c := range s {
		if c == lastChar {
			lastCharCount++
			if lastCharCount >= 3 {
				return true
			}
		} else {
			lastChar = c
			lastCharCount = 1
		}
	}

	return false
}

结果:

blablaaaabla 是否无效?true
blabla 是否无效?false
bla∞∞bla 是否无效?false
bla∞∞∞bla 是否无效?true

以上是给定代码的翻译结果。

英文:

https://play.golang.org/p/HeK3f4uEvz

package main

import &quot;fmt&quot;

func main() {

	// invalid password with repetition
	password := &quot;blablaaaabla&quot;
	fmt.Printf(&quot;%s invalid ? %+v\n&quot;, password, invalid(password))

	// correct password with no repetition
	password = &quot;blabla&quot;
	fmt.Printf(&quot;%s invalid ? %+v\n&quot;, password, invalid(password))

	// valid password with unicode character
	password = &quot;bla∞∞bla&quot;
	fmt.Printf(&quot;%s invalid ? %+v\n&quot;, password, invalid(password))

	// invalid password with unicode character
	password = &quot;bla∞∞∞bla&quot;
	fmt.Printf(&quot;%s invalid ? %+v\n&quot;, password, invalid(password))
}

func invalid(s string) bool {
	var lastChar rune
	var lastCharCount = 0
	for _, c := range s {
		if c == lastChar {
			lastCharCount++
			if lastCharCount &gt;= 3 {
				return true
			}
		} else {
			lastChar = c
			lastCharCount = 1
		}
	}

	return false
}

results

blablaaaabla invalid ? true
blabla invalid ? false
bla∞∞bla invalid ? false
bla∞∞∞bla invalid ? true

答案2

得分: 1

这段代码的功能是检查字符串中是否存在连续重复的字符,并且重复的次数不能超过给定的最大值。以下是翻译好的代码:

func AbideMaxRepetitions(max int, s string) bool {
    var rr rune
    rc := 0
    for _, r := range s {
        if rr == r {
            rc++
            if rc > max {
                return false
            }
        } else {
            rr = r
            rc = 1
        }
    }
    return true
}

func main() {
    ss := []string{"pass", "paaass", "paaaaaaaaaass", "ppa", "pppa", "x", "xx", "xxx"}
    for _, s := range ss {
        fmt.Printf("%q: %v\n", s, AbideMaxRepetitions(2, s))
    }
}

运行结果如下:

"pass": true
"paaass": false
"paaaaaaaaaass": false
"ppa": true
"pppa": false
"x": true
"xx": true
"xxx": false
英文:

This works:

func AbideMaxRepetitions(max int, s string) bool {
	var rr rune
	rc := 0
	for _, r := range s {
		if rr == r {
			rc++
			if rc &gt; max {
				return false
			}
		} else {
			rr = r
			rc = 1
		}

	}
	return true
}

When running:

func main() {
	ss := []string{&quot;pass&quot;, &quot;paaass&quot;, &quot;paaaaaaaaaass&quot;, &quot;ppa&quot;, &quot;pppa&quot;, &quot;x&quot;, &quot;xx&quot;, &quot;xxx&quot;}
	for _, s := range ss {
		fmt.Printf(&quot;%q: %v\n&quot;, s, AbideMaxRepetitions(2, s))
	}
}

Prints:

&quot;pass&quot;: true
&quot;paaass&quot;: false
&quot;paaaaaaaaaass&quot;: false
&quot;ppa&quot;: true
&quot;pppa&quot;: false
&quot;x&quot;: true
&quot;xx&quot;: true
&quot;xxx&quot;: false

答案3

得分: 0

一个简单的循环就可以解决问题。只需记住Go使用UTF8编码来表示字符串。你需要比较符文(字符),而不是字节。

英文:

A simple loop should do the trick. Just remember that Go uses UTF8 for strings. You need to compare Runes (character) not bytes.

huangapple
  • 本文由 发表于 2017年1月19日 01:24:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/41725729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定