C++ solution with using transform template funtion

  • 0
    class Solution {
    bool detectCapitalUse(string word) {
            string allC, firstC, allt;
            transform(word.begin(), word.end(), back_inserter(allC), ::toupper); 
            transform(word.begin()+1, word.end(), back_inserter(firstC), ::tolower);
            firstC = word[0]+firstC;
            transform(word.begin(), word.end(), back_inserter(allt), ::tolower);
            if(word == allC || word ==  firstC || word == allt)
                return true;
            return false;

    Using transform template funtion to transform letter in string , it will get new word. And we compare it with oringinal word.But the runtime is 12 ms. How to further reduce the time complexity?

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.