字符串包含形式为1(0+)1的模式,其中(0+)表示0的任何非空连续序列。计算所有此类模式。这些图案可以重叠。 注: 它只包含数字和小写字符。字符串不一定是二进制的。100201不是有效的模式。 本文讨论了解决该问题的一种方法,中给出了使用正则表达式的其他方法 第二组 例如:
null
Input : 1101001Output : 2Input : 100001abc101Output : 2
让输入字符串的大小为n。 1.遍历索引“0”到“n-1”。 2.如果我们遇到一个“1”,我们迭代直到元素为“0”。 3.在零流结束后,我们检查是否遇到“1”。 4.继续这样做直到我们到达终点。 下面是上述方法的实现。
C++
/* Code to count 1(0+)1 patterns in a string */ #include <bits/stdc++.h> using namespace std; /* Function to count patterns */ int patternCount(string str) { /* Variable to store the last character*/ char last = str[0]; int i = 1, counter = 0; while (i < str.size()) { /* We found 0 and last character was '1', state change*/ if (str[i] == '0' && last == '1' ) { while (str[i] == '0' ) i++; /* After the stream of 0's, we got a '1', counter incremented*/ if (str[i] == '1' ) counter++; } /* Last character stored */ last = str[i]; i++; } return counter; } /* Driver Code */ int main() { string str = "1001ab010abc01001" ; cout << patternCount(str) << endl; return 0; } |
JAVA
// Java Code to count 1(0+)1 // patterns in a string import java.io.*; class GFG { // Function to count patterns static int patternCount(String str) { /* Variable to store the last character*/ char last = str.charAt( 0 ); int i = 1 , counter = 0 ; while (i < str.length()) { /* We found 0 and last character was '1', state change*/ if (str.charAt(i) == '0' && last == '1' ) { while (str.charAt(i) == '0' ) i++; // After the stream of 0's, we // got a '1',counter incremented if (str.charAt(i) == '1' ) counter++; } /* Last character stored */ last = str.charAt(i); i++; } return counter; } // Driver Code public static void main (String[] args) { String str = "1001ab010abc01001" ; System.out.println(patternCount(str)); } } // This code is contributed by vt_m. |
Python3
# Python3 code to count 1(0+)1 patterns in a # Function to count patterns def patternCount( str ): # Variable to store the last character last = str [ 0 ] i = 1 ; counter = 0 while (i < len ( str )): # We found 0 and last character was '1', # state change if ( str [i] = = '0' and last = = '1' ): while ( str [i] = = '0' ): i + = 1 # After the stream of 0's, we got a '1', # counter incremented if ( str [i] = = '1' ): counter + = 1 # Last character stored last = str [i] i + = 1 return counter # Driver Code str = "1001ab010abc01001" ans = patternCount( str ) print (ans) # This code is contributed by saloni1297 |
C#
// C# Code to count 1(0 + )1 // patterns in a string using System; class GFG { // Function to count patterns static int patternCount(String str) { // Variable to store the // last character char last = str[0]; int i = 1, counter = 0; while (i < str.Length) { // We found 0 and last // character was '1', // state change if (str[i] == '0' && last == '1' ) { while (str[i] == '0' ) i++; // After the stream of 0's, we // got a '1',counter incremented if (str[i] == '1' ) counter++; } // Last character stored last = str[i]; i++; } return counter; } // Driver Code public static void Main () { String str = "1001ab010abc01001" ; Console.Write(patternCount(str)); } } // This code is contributed by nitin mittal |
PHP
<?php // PHP Code to count 1(0+)1 patterns // in a string // Function to count patterns function patternCount( $str ) { // Variable to store the // last character $last = $str [0]; $i = 1; $counter = 0; while ( $i < strlen ( $str )) { // We found 0 and last character // was '1', state change if ( $str [ $i ] == '0' && $last == '1' ) { while ( $str [ $i ] == '0' ) $i ++; // After the stream of 0's, // we got a '1', counter // incremented if ( $str [ $i ] == '1' ) $counter ++; } /* Last character stored */ $last = $str [ $i ]; $i ++; } return $counter ; } // Driver Code $str = "1001ab010abc01001" ; echo patternCount( $str ) ; // This code is contributed by nitin mittal ?> |
Javascript
<script> // javascript Code to count 1(0+)1 // patterns in a string // Function to count patterns function patternCount(str) { /* Variable to store the last character*/ var last = str.charAt(0); var i = 1, counter = 0; while (i < str.length) { /* We found 0 and last character was '1', state change*/ if (str.charAt(i) == '0' && last == '1' ) { while (str.charAt(i) == '0' ) i++; // After the stream of 0's, we // got a '1',counter incremented if (str.charAt(i) == '1') counter++; } /* Last character stored */ last = str.charAt(i); i++; } return counter; } // Driver Code var str = "1001ab010abc01001" ; document.write(patternCount(str)); // This code is contributed by 29AjayKumar </script> |
输出:
2
本文由 罗希特·塔普里亚尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END