# Check if a given mobile number is fancy

Given a mobile number and some conditions for a fancy number, find if the given number is fancy. A 10 digit mobile number is called fancy if it satisfies any of the following three conditions.

1. A single number occurs three consecutive times. Like 777.
2. Three consecutive digits are either in increasing or decreasing fashion. Like 456 or 987.
3. A single digit occurs four or more times in the number. Like 9859009976 – here the digit 9 occurs 4 times.

Examples:

Input : 9859009976
Output : Yes
The given mobile number satisfies condition
three given above.

Input : 7609438921
Output : No
None of the given three conditions satisfy.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Idea is to convert number into string using to_string so that it becomes easy to traverse. For condition three to count the frequency of every number a basic concept of string hashing is used.

Source :Oracle Interview Set

Below is the solution of above problem.

## C++

 `// C++ program to check if a given mobile ` `// number is fancy or not. ` `#include ` `using` `namespace` `std; ` ` `  `// Returns true if s has three consecutive  ` `// same digits. ` `bool` `cond1(string s) ` `{ ` `    ``for` `(``int` `i = 0; i < s.size() - 2; i++) { ` `        ``if` `(s[i] == s[i + 1] && s[i + 1] == s[i + 2]) ` `            ``return` `true``; ` `    ``} ` `    ``return` `false``; ` `} ` ` `  `// Returns true if s has three increasing or ` `// decreasing digits. ` `bool` `cond2(string s) ` `{ ` `    ``for` `(``int` `i = 0; i < s.size() - 2; i++) { ` `        ``if` `((s[i] < s[i + 1] && s[i + 1] < s[i + 2]) ||  ` `            ``(s[i] > s[i + 1] && s[i + 1] > s[i + 2])) ` `            ``return` `true``; ` `    ``} ` `    ``return` `false``; ` `} ` ` `  `// Checks if a single digit occurs 4 times. ` `bool` `cond3(string s) ` `{ ` `    ``int` `a; ` `    ``memset``(a, 0, ``sizeof``(a)); ` ` `  `    ``for` `(``int` `i = 0; i < s.size(); i++)  ` `        ``a[s[i] - ``'0'``]++; ` `    `  `    ``for` `(``int` `i = 0; i < 9; i++)  ` `        ``if` `(a[i] >= 4) ` `            ``return` `true``; ` `     `  `    ``return` `false``; ` `} ` ` `  `bool` `isFancy(string s) ` `{ ` `    ``if` `(cond1(s) || cond2(s) || cond3(s)) ` `        ``return` `true``; ` `    ``else` `        ``return` `false``; ` `} ` ` `  `// Driver condition ` `int` `main() ` `{ ` `    ``long` `int` `n = 7609438921;  ` `    ``string s = to_string(n); ` `    ``if` `(isFancy(s)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to check if a given mobile ` `// number is fancy or not. ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` `  `class` `GFG { ` `     `  `public` `static` `void` `main(String[] args) { ` `         `  `        ``String mobileNumber = ``"7654449244"``; ` `         `  `        ``if` `(isFancy(mobileNumber)) ` `            ``System.out.println(``"Yes"``); ` `        ``else` `            ``System.out.println(``"No"``); ` `    ``} ` ` `  `    ``public` `static` `boolean` `isFancy(String mobileNumber) { ` `        ``int` `incrementCount = ``0``; ` `        ``int` `decrementCount = ``0``; ` `        ``int` `consecutiveCount = ``1``; ` `        ``int``[] countArray = ``new` `int``[``10``]; ` `        ``int` `prevDigit = -``1``; ` `        ``for``(``int` `i = ``0``; i < mobileNumber.length(); i++) { ` `         `  `            ``int` `digit = Integer.parseInt(String.valueOf(mobileNumber.charAt(i))); ` `            ``countArray[digit] += ``1``; ` `            ``// Checking for Number of occurrences of any digit is greater than 3 ` `         `  `            ``if``(countArray[digit] > ``3``) ` `                ``return` `true``; ` `            ``// Checking for consecutive digits are same ` `         `  `            ``if``(prevDigit == digit) ` `                ``consecutiveCount += ``1``; ` `         `  `            ``else` `if``(prevDigit == digit+``1` `&& prevDigit != -``1``) { ` `                ``incrementCount += ``1``; ` `                ``decrementCount = ``0``; ` `                ``consecutiveCount = ``1``; ` `            ``} ` `         `  `            ``else` `if``(digit == prevDigit+``1``) { ` `                ``decrementCount += ``1``; ` `                ``incrementCount = ``0``; ` `                ``consecutiveCount = ``1``; ` `            ``} ` `         `  `            ``if``(consecutiveCount == ``3``) ` `                ``return` `true``; ` `         `  `            ``if``(incrementCount == ``2` `|| decrementCount == ``2``) ` `                ``return` `true``; ` `         `  `            ``prevDigit = digit; ` `        ``} ` `        ``return` `false``; ` `    ``} ` `} ` ` `  `// This code is contributed by Vasishta Balla `

## Python 3

 `# Python3 program to check if a  ` `# given mobile number is fancy or not.  ` ` `  `# Returns true if s has three  ` `# consecutive same digits.  ` `def` `cond1(s):  ` ` `  `    ``for` `i ``in` `range``(``len``(s) ``-` `2``):  ` `        ``if` `(s[i] ``=``=` `s[i ``+` `1``] ``and`  `            ``s[i ``+` `1``] ``=``=` `s[i ``+` `2``]):  ` `            ``return` `True` `     `  `    ``return` `False` ` `  `# Returns true if s has three  ` `# increasing or decreasing digits.  ` `def` `cond2(s):  ` `    ``for` `i ``in` `range``(``len``(s) ``-` `2``):  ` `        ``if` `((s[i] < s[i ``+` `1``] ``and`  `             ``s[i ``+` `1``] < s[i ``+` `2``]) ``or`  `            ``(s[i] > s[i ``+` `1``] ``and`  `             ``s[i ``+` `1``] > s[i ``+` `2``])):  ` `            ``return` `True` `     `  `    ``return` `False` ` `  `# Checks if a single digit  ` `# occurs 4 times.  ` `def` `cond3(s):  ` `    ``a ``=` `[``0``] ``*` `10` `    ``for` `i ``in` `range``(``len``(s)):  ` `        ``a[s[i] ``-` `'0'``] ``=` `a[s[i] ``-` `'0'``] ``+` `1` `     `  `    ``for` `i ``in` `range``(``len``(``9``)):  ` `     `  `        ``if` `(a[i] >``=` `4``):  ` `            ``return` `True` `     `  `    ``return` `False` ` `  `def` `isFancy(s):  ` `    ``if` `(cond1(s) ``or` `cond2(s) ``or` `cond3(s)):  ` `        ``return` `True` `    ``else``: ` `        ``return` `False` ` `  `# Driver condition  ` `s ``=` `"7609438921"` `if` `(isFancy(s)):  ` `    ``print``(``"Yes"``)  ` `else``: ` `    ``print``(``"No"``) ` `     `  `# This code is contributed by ash264 `

## C#

 `// C# program to check if a given mobile ` `// number is fancy or not. ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` ` `  `        ``String mobileNumber = ``"7654449244"``; ` ` `  `        ``if` `(isFancy(mobileNumber))  ` `        ``{ ` `            ``Console.WriteLine(``"Yes"``); ` `        ``}  ` `        ``else` `        ``{ ` `            ``Console.WriteLine(``"No"``); ` `        ``} ` `    ``} ` ` `  `    ``public` `static` `bool` `isFancy(String mobileNumber) ` `    ``{ ` `        ``int` `incrementCount = 0; ` `        ``int` `decrementCount = 0; ` `        ``int` `consecutiveCount = 1; ` `        ``int``[] countArray = ``new` `int``; ` `        ``int` `prevDigit = -1; ` `        ``for` `(``int` `i = 0; i < mobileNumber.Length; i++) ` `        ``{ ` ` `  `            ``int` `digit = Int32.Parse(String.Join(``""``,mobileNumber[i])); ` `            ``countArray[digit] += 1; ` `             `  `            ``// Checking for Number of occurrences ` `            ``// of any digit is greater than 3 ` `            ``if` `(countArray[digit] > 3) ` `            ``{ ` `                ``return` `true``; ` `            ``} ` ` `  `            ``// Checking for consecutive digits are same ` `            ``if` `(prevDigit == digit)  ` `            ``{ ` `                ``consecutiveCount += 1; ` `            ``}  ` `            ``else` `if` `(prevDigit == digit + 1 && prevDigit != -1)  ` `            ``{ ` `                ``incrementCount += 1; ` `                ``decrementCount = 0; ` `                ``consecutiveCount = 1; ` `            ``} ` `            ``else` `if` `(digit == prevDigit + 1)  ` `            ``{ ` `                ``decrementCount += 1; ` `                ``incrementCount = 0; ` `                ``consecutiveCount = 1; ` `            ``} ` `            ``if` `(consecutiveCount == 3)  ` `            ``{ ` `                ``return` `true``; ` `            ``} ` `            ``if` `(incrementCount == 2 || decrementCount == 2)  ` `            ``{ ` `                ``return` `true``; ` `            ``} ` `            ``prevDigit = digit; ` `        ``} ` `        ``return` `false``; ` `    ``} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```Yes
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

6

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.