Circular Sentence

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) {
// Step 1: Check if the first character of the sentence is the same as the last character
if(sentence.charAt(0) != sentence.charAt(sentence.length() - 1)){
return false; // If not, it's not a circular sentence
}
// Step 2: Split the sentence into words based on spaces
String[] str = sentence.split(" ");
int length = str.length; // Get the number of words
// Step 3: Initialize wordLastChar as the last character of the first word
char wordLastChar = str[0].charAt(str[0].length() - 1);
// Step 4: Loop through the words starting from the second word
for(int i = 1; i < str.length; i++){
// If the last character of the previous word doesn't match the first character of the current word, return false
if(wordLastChar != str[i].charAt(0)){
return false;
} else {
// Update wordLastChar to the last character of the current word
wordLastChar = str[i].charAt(str[i].length() - 1);
}
}
// Step 5: If all words are linked correctly, return true
return true;
}
}




