Delivered instantly as a PDF via email. For Software Engineers & SDETs
150 programming interview questions and answers
5 proven approaches to crack algorithm questions
10 mistakes candidates make, and how to avoid them.
How to prepare for technical and behavioral questions without wasting your time!
"The BEST book for acing your interview. It helped me land my Microsoft job, and it was worth every penny!" - Ravi (Accepted at Microsoft, Amazon and Facebook)
30 Day Money Back Guarantee: Don't love the book? We'll give you your money back!More Info
Saturday, April 24, 2010
Given a string, find the first unrepeated character in it? Give some test cases.
private void findNonRepeatedCharacter(string givenString) { Console.WriteLine("The given String is {0}", givenString); int[] givenArrayEncoded = new int[256]; int[] givenArrayCharacters = new int[givenString.Length];
for (int iCounter = 0; iCounter < givenString.Length; iCounter++) { int value = System.Convert.ToInt32(givenString[iCounter]); givenArrayEncoded[value]++; givenArrayCharacters[iCounter] = value; }
for (int iCounter = 0; iCounter < givenArrayCharacters.Length; iCounter++) { if (givenArrayEncoded[givenArrayCharacters[iCounter]] == 1) { Console.WriteLine("First character which is not repeated in the string is '{0}'", System.Convert.ToChar(givenArrayCharacters[iCounter])); break; } } }
C# code with O(n)
ReplyDeleteprivate void findNonRepeatedCharacter(string givenString)
{
Console.WriteLine("The given String is {0}", givenString);
int[] givenArrayEncoded = new int[256];
int[] givenArrayCharacters = new int[givenString.Length];
for (int iCounter = 0; iCounter < givenString.Length; iCounter++)
{
int value = System.Convert.ToInt32(givenString[iCounter]);
givenArrayEncoded[value]++;
givenArrayCharacters[iCounter] = value;
}
for (int iCounter = 0; iCounter < givenArrayCharacters.Length; iCounter++)
{
if (givenArrayEncoded[givenArrayCharacters[iCounter]] == 1)
{
Console.WriteLine("First character which is not repeated in the string is '{0}'", System.Convert.ToChar(givenArrayCharacters[iCounter]));
break;
}
}
}