Question System.IndexOutOfRangeException

Wkesther

Member
Joined
Aug 6, 2022
Messages
24
Programming Experience
Beginner
Hello, I am trying to write a method, that checks if a string is isomorphic and returns true or false. In line 10 in the code posted below, I get a System.OutOfRangeException and I just can't figure out what I am doing false.

C#:
public static bool IsIsomorphic(string s, string t)
        {
            bool isomorphic = false;
            string endstring = "";

            if (t.Length != s.Length)
                return false;
            for (int i = 1; i <= s.Length; i++)
            {
                if (s.IndexOf(s[i]) != s.LastIndexOf(s, i) && t[s.IndexOf(s[i])] == t[s.LastIndexOf(s, i)] || s.IndexOf(s[i]) == s.LastIndexOf(s, i))
                {
                    endstring += s[i];
                }
            }
            if (endstring.Length == s.Length)
                isomorphic = true;
            else
                isomorphic = false;

            return isomorphic;
        }

        static void Main(string[] args)
        {
            Console.WriteLine(IsIsomorphic("siinger", "solnaer"));
            Console.ReadKey();
        }
 
s.Length is not a valid index, zero-based indexing goes from 0 to length-1.
 
Thank you for your answers. Not knowing what to do to fix this and how to get a good solution, I searched the internet for a solution and also found a working one. Now I just can't understand the logic of this solution. Can someone of you explain the logic of this solution to me? That would be really nice.

C#:
public static bool IsIsomorphic(string s, string t)
        {
            int[] occuranceS = Enumerable.Repeat(-1, 256).ToArray(),
                occuranceT = Enumerable.Repeat(-1, 256).ToArray();

            for (int i = 0; i < s.Length; i++)
            {
                if (occuranceS[s[i]] != occuranceT[t[i]])
                    return false;

                occuranceS[s[i]] = occuranceT[t[i]] = i;
            }

            return true;
        }

        static void Main(string[] args)
        {
            Console.WriteLine(IsIsomorphic("saanger", "solnaer"));
            Console.ReadKey();
        }
 
You should start by reading the documentation for any types or members you're not familiar with, e.g. Enumerable.Range. VS supports context-sensitive Help, as Windows has done for decades, so you can click a name in code and press F1 to go straight to the relevant documentation.

Next, you should execute the code in the debugger and step through it line by line, evaluating relevant variables and expressions as you do, to see exactly what the code does. If you're using a method called IsIsomorphic then I would hope that you have some idea of what the code is going to be doing. Once you've done all that you can fore yourself, then you can ask us specific questions about the specific parts that you still don't understand.
 
As an aside, the code that you found assumes that C# characters are only 8 bits. C# characters are 16 bits.
 
As another aside, that "found code" in post #4 has an interesting bug.it thinks that "left" and "right" are isomorphic.
 
It's not a matter of looking for another solution online as you have been doing. It is a matter of understanding the problem and writing your own code. That is the point of assignments and projects given to you in school or training classes. It is to force you to analyze and break down the problem, come up with a solution, and then write code to implement that solution. It is not meant to be a test of your Google skills.

Stop and think. How will you solve this problem if you did not have a computer and only have pen and paper. Next describe those steps you took to solve the problem. Next imagine that you are talking to a 5 year old on the phone. Try to give simple instructions to him/her so that they can solve the problem with you giving the instructions. That set of simple steps will be the basis for your code.
 
Back
Top Bottom