Microsoft Interview Question
Software Engineer in TestsCountry: United States
Interview Type: In-Person
//O(n)
int Palindromes(char[] s)
{
int count=0;
int m=s.Length/2;
if((s==null)||(s.Length==0))
{
return 0;
}
else{
if(s.Length%2==1)
{
int i = 1;
while((i+1)<s.Length)
{
if (s[i - 1] == s[i + 1])
{
count++;
}
i++;
}
}else{
int i = 2;
while ((i+1) < s.Length)
{
if ((s[i - 1] == s[i]) && (s[i + 1] == s[i - 2]))
{
count++;
}
i++;
}
}
} return count;
}
Per my understanding on the problem, we need to find out whether each word in a sentense is palindrome or not, then display the palindromes. Identifying each word is by separating by comma or space etc., But this program is only verifying the adjacent characters are equal or not, but it is not verifyinig palindromes in single sentense also. Please correct if I am wrong.
1.Spilt the string by delimiters
2.reverse the every word in string
3.compare two strings
static void Main(string[] args)
{
int count = 0;
string s = "dad mam hahah hi";
string[] words = s.Split(' ', ',');
string[] reverseWord = new string[words.Length];
for (int i=0; i<words.Length; i++)
{
reverseWord[i] = reverseString(words[i].ToCharArray(), 0, words[i].Length - 1);
}
Console.WriteLine("Given sententce =" + s);
for (int i=0; i<words.Length; i++)
{
for (int j=i; i<reverseWord.Length; )
{
if (words[i] == reverseWord[j])
{
Console.WriteLine(words[i]);
count++;
break;
}
else
break;
}
}
Console.WriteLine("total Palindromes in sententce =" + count);
Console.ReadLine();
}
static string reverseString(char[] sentence, int lindex, int rindex)
{
string s = string.Empty;
while (lindex < rindex)
{
char tempChar = sentence[lindex];
sentence[lindex++] = sentence[rindex];
sentence[rindex--] = tempChar;
}
s = new string(sentence);
return s;
}
1.Spilt the string by words using delimiters
2.Push the every word by character
3.Pop the each character and compare with word.
static void Main(string[] args)
{
int count = 0;
int charCount = 0;
Stack<char> charStack = new Stack<char>();
string s = "hi pop civic hello kayak";
string[] words = s.Split(' ', ',');
for (int i = 0; i < words.Length; i++)
{
char[] word = words[i].ToCharArray();
foreach (char c in word)
{
charStack.Push(c);
}
foreach (char c in word)
{
char popchar = charStack.Pop();
if (popchar == c)
{
charCount++;
}
}
if (words[i].Length == charCount)
{
count++;
charCount = 0;
}
else
charCount = 0;
}
Console.WriteLine("Given sententce = " + s);
Console.WriteLine("Total Palindromes in sententce = " + count);
Console.ReadLine();
}
int countPalindromes(const string& sentence)
{
int cnt = 0;
int i(0), j(0), n = sentence.size();
while (i < n && sentence[i]==' ') i++;
while (i < n) {
j = i;
while (j < n && sentence[i] != ' ) j++;
cnt += (isPalin(sentence, i, j-1) ? 1 : 0);
i = j+1;
}
}
bool isPalin(const string& str, int i, int j)
{
while (i < j) {
if (str[i] != str[j]) return false;
i++; j--;
}
return true;
}
Use a stack.
- Learner January 18, 2012-Start reading the input string character by character.
-PUSH a character from string onto stack if it doesnt match the character at the stack top.
-POP a character from stack if it input character matches it.
-Count the number of times the stack size reaches 0. The input string has these many number of palindromes (Palindromes within a palindrome do come under this count)