String has same characters at same position
You are given an arrty of S consisting of N strings. Every string is of the same length M. YOUr task is to find a pair of string in array S, such that there exists a position in which both of the strins have the same letter. Both the index in array S and the position in the string are numbered from zero. For example given S=["Abc","aca","abe"]. String "abc" and string "dba" have same character "b" in position 1
public class Solution
{
public int[] solution(string[] S)
{
//ensure string is not null here
if (S?.Length > 0)
{
Dictionary<char, Dictionary<int, int>> dict = [];
for (int i = 0; i < S.Length; i++)
{
for (int j = 0; j < S[i].Length; j++)
{
if (!dict.ContainsKey(S[i][j]))
{
dict[S[i][j]] = [];
}
if (dict[S[i][j]].ContainsKey(j))
{
return [dict[S[i][j]][j], i, j];
}
else
{
dict[S[i][j]][j] = i;
}
}
}
}
return [];
}
}
Last updated