Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to make this code more efficient? (C#)

frowNYfacefrowNYface Member
edited September 2020 in Programming

So, I was practicing C# on edabit.com and I came across this question which I really liked and I did end up solving it, but I don't think so that this code is efficient.

Link To The Question: https://edabit.com/challenge/RGwqnBMmbDKJpZs2a

Code:

using System;


class Program
{
    public static int BridgesII(string str)
    {
        int count = 0; //Will count all the times a bridge is not there
        string[] arr = str.Split('/');
// Traversing Horizontally Through The Grid

        for (int index = 0; index < arr.Length; index++) // Traversing Through The Elements of The Array
        {
            for (int i = 0; i < arr[index].Length; i++) // Traversing Through The Elements of The Elements
            {
                if (arr[index][i] == ' ')
                {
                    count++;
                    break;
                }
                else
                {
                    continue;
                }
            }
        }
// Traversing Vertically Through The Grid

        for (int j = 0; j < arr.Length; j++) // Traversing Through The Elements of The Elements
        {
            for (int k = 0; k < arr.Length; k++) // Traversing Through The Elements of The Array
            {
                if (arr[k][j] == ' ')
                {
                    count += 1;
                    break;
                }
                else
                {
                    continue;
                }
            }
        }
        return (2 * arr.Length) - count;
    }
}

Please tell me, if any efficient code already exists! Thanks. 😊

Edit: I have also added comments.

Sign In or Register to comment.