Latest Coding questions asked in TOP MNCs for Freshers and Experienced candidates
![<code>
// C# function to determine if two strings differ by exactly one character
public static bool FuncFoo(string s1, string s2)
{
int m = s1.Length;
int n = s2.Length;
// Check if the absolute length difference is exactly 1
if (Math.Abs(m - n) != 1)
{
return false;
}
int i = 0;
int j = 0;
int count = 0;
// Iterate through the strings, counting differences
while (i < m && j < n)
{
if (s1[i] == s2[j])
{
i++;
j++;
}
else
{
count++;
// Adjust indices based on length comparison
if (m > n)
{
i++;
}
else if (n > m)
{
j++;
}
else
{
i++;
j++;
}
}
}
// Check if there is exactly one difference or if lengths are equal
if (count == 1 || (count == 0 && m != n))
{
return true;
}
return false;
}
</code>](https://static.wixstatic.com/media/d96e38_8882d230e6bf40e78a5a631001f415f2~mv2.jpg/v1/fill/w_579,h_312,al_c,q_80,usm_0.66_1.00_0.01,enc_avif,quality_auto/Image-place-holder.jpg)
1) what does the below function achieve and illustrate your understanding with an example below:
Tell true or false for below inputs:
input strings are "abc123" and "abc1234"
input strings are "abc1234" and "abc123"
input strings are "abc1234" and "abc321"
<code>
// C# function to determine if two strings differ by exactly one character
public static bool FuncFoo(string s1, string s2)
{
int m = s1.Length;
int n = s2.Length;
// Check if the absolute length difference is exactly 1
if (Math.Abs(m - n) != 1)
{
return false;
}
int i = 0;
int j = 0;
int count = 0;
// Iterate through the strings, counting differences
while (i < m && j < n)
{
if (s1[i] == s2[j])
{
i++;
j++;
}
else
{
count++;
// Adjust indices based on length comparison
if (m > n)
{
i++;
}
else if (n > m)
{
j++;
}
else
{
i++;
j++;
}
}
}
// Check if there is exactly one difference or if lengths are equal
if (count == 1 || (count == 0 && m != n))
{
return true;
}
return false;
}
</code>
Latest News