Created
March 13, 2016 06:25
-
-
Save jianminchen/aa85318f91fbcdc8f74d to your computer and use it in GitHub Desktop.
HackerRank - GemStones - https://www.hackerrank.com/challenges/gem-stones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Gemstones | |
{ | |
/* | |
* Gemstones | |
* https://www.hackerrank.com/challenges/gem-stones | |
* | |
*/ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string s = Console.ReadLine(); | |
int n = Convert.ToInt16(s); | |
string[] sA = new string[n]; | |
for(int i=0; i<n;i++) | |
{ | |
sA[i]= Console.ReadLine(); | |
} | |
Console.WriteLine(getNumberOfGemElement(sA)); | |
} | |
/* | |
* string length >=1, <=100 | |
* only lower-cse Latin letters 'a'-'z' | |
* | |
* string array size 1 - 100 | |
*/ | |
public static int getNumberOfGemElement(string[] s) | |
{ | |
int len = s.Length; | |
int[][] countA = new int[len][]; | |
int Size = 26; | |
for (int i = 0; i < len; i++ ) | |
{ | |
countA[i] = new int[Size]; | |
} | |
for (int i = 0; i < len; i++) | |
{ | |
string s1 = s[i]; | |
foreach(char c in s1) | |
{ | |
countA[i][c - 'a']++; | |
} | |
} | |
int number = 0; | |
for (int i = 0; i < Size; i++) | |
{ | |
bool atLeastOneZero = false; | |
for (int j = 0; j < len; j++) // bug002 not i++, should be j++; spell error | |
{ | |
//if (countA[i][j] == 0) // bug001 - index out of range | |
if (countA[j][i] == 0) | |
{ | |
atLeastOneZero = true; | |
break; | |
} | |
} | |
if (!atLeastOneZero) | |
number++; | |
} | |
return number; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment