LeetCode·Daily Question·2490. Loopback Sentence·Simulation

Author: Xiao Xun
Link: https://leetcode.cn/problems/circular-sentence/solutions/2325227/mo-ni-zhu-shi-chao-ji-xiang-xi-by-xun-ge-x65e/
Source: The copyright of LeetCode
belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.

topic

 

example

 

train of thought

Title -> Given a string, determine whether the string is a loopback sentence

A sentence is considered a loopback sentence if it satisfies all of the following conditions:

  • 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.

Simulate directly according to the meaning of the question, first take the beginning and end characters of the string, judge whether the requirements are met, and then enumerate the string, when encountering ' ', judge whether the previous character is equal to the next character, if the above conditions are met, returns TRUE.

Code comments are super detailed

the code


bool isCircularSentence(char * sentence){
    int len = strlen(sentence);
    char start = sentence[0], end = sentence[len-1];
    if (start != end) return false;//先比较开始和结尾位置
    for (int i = 0; i < len; ++i) {//枚举字符串
        if (sentence[i] == ' ' && sentence[i-1] != sentence[i+1]) return false;//不满足要求二
    }
    return true;//上述条件都满足
}

作者:小迅
链接:https://leetcode.cn/problems/circular-sentence/solutions/2325227/mo-ni-zhu-shi-chao-ji-xiang-xi-by-xun-ge-x65e/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Guess you like

Origin blog.csdn.net/m0_64560763/article/details/131469397