Skip to main content

Command Palette

Search for a command to run...

Circular Sentence

Published
2 min read
Circular Sentence
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

A sentence is a list of words that are separated by a single space with no leading or trailing spaces.

  • For example, "Hello World", "HELLO", "hello world hello world" are all sentences.

Words consist of only uppercase and lowercase English letters. Uppercase and lowercase English letters are considered different.

A sentence is circular if:

  • The last character of a word is equal to the first character of the next word.

  • The last character of the last word is equal to the first character of the first word.

For example, "leetcode exercises sound delightful", "eetcode", "leetcode eats soul" are all circular sentences. However, "Leetcode is cool", "happy Leetcode", "Leetcode" and "I like Leetcode" are not circular sentences.

Given a string sentence, return true if it is circular. Otherwise, return false.

LeetCode Problem - 2490

class Solution {
    public boolean isCircularSentence(String sentence) {
        // Check if the first and last characters of the sentence are the same
        if (sentence.charAt(0) != sentence.charAt(sentence.length() - 1))
            return false; // Return false if they are not the same

        // Split the sentence into words
        String[] arrSent = sentence.split(" ");

        // If there is only one word, check if the first and last characters are the same
        if (arrSent.length == 1) {
            if (sentence.charAt(0) != sentence.charAt(sentence.length() - 1))
                return false; // Return false if they are not the same
        } else {
            // Initialize the last character of the first word
            char lastChar = arrSent[0].charAt(arrSent[0].length() - 1);

            // Iterate through each word starting from the second one
            for (int i = 1; i < arrSent.length; i++) {
                // Get the first character of the current word
                char currChar = arrSent[i].charAt(0);

                // If the last character of the previous word does not match the first character of the current word, return false
                if (lastChar != currChar)
                    return false;
                else {
                    // Update the last character for the next comparison
                    lastChar = arrSent[i].charAt(arrSent[i].length() - 1);
                }
            }
        }

        // Return true if the sentence meets the circular condition
        return true;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.