DDSA Solutions

2490. Circular Sentence

Time: O(n)
Space: O(1)
Advertisement

Intuition

Check if word forms a valid zigzag: letters alternate strictly up and down. Check direction changes.

Algorithm

  1. 1Track direction (up/down). On equal adjacent chars: not zigzag. On direction change: verify it alternates.

Common Pitfalls

  • Direction must alternate at each step. Any two consecutive equal chars invalidate it.
2490.cs
C#
// Approach: Check each word boundary where last char of prev equals first char of next; wrap around.
// Time: O(n) Space: O(1)

public class Solution
{
    public bool IsCircularSentence(string sentence)
    {
        for (int i = 0; i < sentence.Length; ++i)
        {
            if (sentence[i] == ' ' && sentence[i - 1] != sentence[i + 1])
                return false;
        }
        
        return sentence[0] == sentence[sentence.Length - 1];
    }
}
Advertisement
Was this solution helpful?