# Find the number of players who roll the dice when the dice output sequence is given

Given a string **S** and a number **X**. There are **M** players who roll the dice. A player keeps on rolling the dice until he gets a number other than **X**. In the string S, S[i] represents the number at i^{th} roll of a dice. The task is to find **M**. **Note** that the last character in **S** will never be **X**.**Examples:**

Input:s = “3662123”, X = 6Output:5

First player rolls and gets 3.

Second player rolls and gets 6, 6 and 2.

Third player rolls and gets 1.

Fourth player rolls and gets 2.

Fifth player rolls and gets 3.Input:s = “1234223”, X = 2Output:4

**Approach:** Iterate in the string and count the characters which are not **X**. The number of characters which are not **X** will be the number of players.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to return the number of players` `int` `findM(string s, ` `int` `x)` `{` ` ` `// Initialize cnt as 0` ` ` `int` `cnt = 0;` ` ` `// Iterate in the string` ` ` `for` `(` `int` `i = 0; i < s.size(); i++) {` ` ` `// Check for numbers other than x` ` ` `if` `(s[i] - ` `'0'` `!= x)` ` ` `cnt++;` ` ` `}` ` ` `return` `cnt;` `}` `// Driver code` `int` `main()` `{` ` ` `string s = ` `"3662123"` `;` ` ` `int` `x = 6;` ` ` `cout << findM(s, x);` ` ` `return` `0;` `}` |

## Java

`// Java implementation of the approach` `import` `java.util.*;` `class` `GFG` `{` `// Function to return the number of players` `static` `int` `findM(String s, ` `int` `x)` `{` ` ` `// Initialize cnt as 0` ` ` `int` `cnt = ` `0` `;` ` ` `// Iterate in the string` ` ` `for` `(` `int` `i = ` `0` `; i < s.length(); i++)` ` ` `{` ` ` `// Check for numbers other than x` ` ` `if` `(s.charAt(i) - ` `'0'` `!= x)` ` ` `cnt++;` ` ` `}` ` ` `return` `cnt;` `}` `// Driver code` `public` `static` `void` `main(String args[])` `{` ` ` `String s = ` `"3662123"` `;` ` ` `int` `x = ` `6` `;` ` ` `System.out.println(findM(s, x));` `}` `}` `//This code is contributed by` `// Surendra_Gangwar` |

## Python3

`# Python 3 implementation of the approach` `# Function to return the number of players` `def` `findM(s, x):` ` ` ` ` `# Initialize cnt as 0` ` ` `cnt ` `=` `0` ` ` `# Iterate in the string` ` ` `for` `i ` `in` `range` `(` `len` `(s)):` ` ` ` ` `# Check for numbers other than x` ` ` `if` `(` `ord` `(s[i]) ` `-` `ord` `(` `'0'` `) !` `=` `x):` ` ` `cnt ` `+` `=` `1` ` ` `return` `cnt` `# Driver code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `s ` `=` `"3662123"` ` ` `x ` `=` `6` ` ` `print` `(findM(s, x))` ` ` `# This code is contributed by` `# Surendra_Gangwar` |

## C#

`// C# implementation of the approach` `using` `System;` `class` `GFG` `{` `// Function to return the number of players` `static` `int` `findM(String s, ` `int` `x)` `{` ` ` `// Initialize cnt as 0` ` ` `int` `cnt = 0;` ` ` `// Iterate in the string` ` ` `for` `(` `int` `i = 0; i < s.Length; i++)` ` ` `{` ` ` `// Check for numbers other than x` ` ` `if` `(s[i] - ` `'0'` `!= x)` ` ` `cnt++;` ` ` `}` ` ` `return` `cnt;` `}` `// Driver code` `public` `static` `void` `Main()` `{` ` ` `String s = ` `"3662123"` `;` ` ` `int` `x = 6;` ` ` `Console.Write(findM(s, x));` `}` `}` `// This code is contributed by` `// mohit kumar` |

## PHP

`<?php` `// PHP implementation of the approach` `// Function to return the number of players` `function` `findM(` `$s` `, ` `$x` `)` `{` ` ` `// Initialize cnt as 0` ` ` `$cnt` `= 0;` ` ` `// Iterate in the string` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `strlen` `(` `$s` `); ` `$i` `++)` ` ` `{` ` ` `// Check for numbers other than x` ` ` `if` `(ord(` `$s` `[` `$i` `]) - ord(` `'0'` `) != ` `$x` `)` ` ` `$cnt` `++;` ` ` `}` ` ` `return` `$cnt` `;` `}` `// Driver code` `$s` `= ` `"3662123"` `;` `$x` `= 6;` `echo` `findM(` `$s` `, ` `$x` `);` `// This code is contributed by Ryuga` `?>` |

## Javascript

`<script>` `// javascript implementation of the approach` ` ` `// Function to return the number of players` ` ` `function` `findM( s , x) {` ` ` `// Initialize cnt as 0` ` ` `var` `cnt = 0;` ` ` `// Iterate in the string` ` ` `for` `(i = 0; i < s.length; i++) {` ` ` `// Check for numbers other than x` ` ` `if` `(s.charCodeAt(i) - ` `'0'` `.charCodeAt(0) != x)` ` ` `cnt++;` ` ` `}` ` ` `return` `cnt;` ` ` `}` ` ` `// Driver code` ` ` ` ` `var` `s = ` `"3662123"` `;` ` ` `var` `x = 6;` ` ` `document.write(findM(s, x));` `// This code contributed by Rajput-Ji` `</script>` |

**Output:**

5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.