tiwaripradeep123
BAN USERHow HastSet will handle duplicate numbers say set is {1 2 2 10} and sum is 4
HashSet will store only {1 2 10} and it will fail to find any pair with sum 4 or if you say this not fail them it will give false Positive result for sum = 20.
Seems we need yo use Hashtable< number as Key, count as value>
public bool IsPalindrom(string input)
{
if (string.IsNullOrWhiteSpace(input))
{
return false;
}
int startIndex = 0, endIndex = input.Length - 1;
while (startIndex <= endIndex)
{
while (!IsAnAlphabet(input[startIndex]) && startIndex < endIndex)
{
startIndex++;
}
while (!IsAnAlphabet(input[endIndex]) && startIndex < endIndex)
{
endIndex--;
}
int diff = input[startIndex] - input[endIndex];
if (!(diff == 0 || diff == 32 || diff == -32))
{
return false;
}
startIndex++;
endIndex--;
}
return true;
}
public bool IsAnAlphabet(char c)
{
return (65 <= c && c <= 90 || (97 <= c && c <= 122));
}
Only case won`t be handled is when string is all special case say "!!@#!@$@#$@#$", if will true rather than false.
- tiwaripradeep123 June 07, 2014string input = @"The boy run";
StringBuilder builder = new StringBuilder();
int i =0;
string temp;
bool isFirstInsert = true;
while(i< input.length)
{
if(input[i] != ' ')
{
temp= input[i].ToString() + temp;
}
else
{
if(! isFirstInsert)
{
builder.append(" ");
}
builder.append(temp);
temp =string.Empty;
}
i++;
}
string output = builder.ToString();
its still single pass over input data.
- tiwaripradeep123 June 06, 2014We can ask usage of this operation, I my view if we are going to call multiple times then
we can dedup this array and create another one with starting index for each numbers
say if i/p array is 111122334455
then normalize it to i/p ->12345
and index Array -> 046810
find element index in the i/p array and then get the IndexArray[index] value..
any other solution ?
and if its one time activity then binary search and then move toward left to locate 1st, if found
what if you take int array rather than bool
- tiwaripradeep123 January 28, 2015int[] alphabet = new int[256];
and clause like
if(if(alphabet[s.charAt(i)] > 0 ))
then calculate if this substring is > than previous max and continue ..