thảo luận Leetcode mỗi ngày

  • Người tạo chủ đề Người tạo chủ đề _Gia_Cat_Luong_
  • Ngày bắt đầu Ngày bắt đầu
Trạng thái
Không mở để trả lời thêm.
Mã:
class Solution:
    def makeFancyString(self, s: str) -> str:
        pre = None
        count = 0
        res = ''
        for char in s:
            if pre != char:
                pre = char
                res+=char
                count = 1
            else:
                count+=1
                if count<3: res+=char
        return res
 
Python:
class Solution:
    def makeFancyString(self, s: str) -> str:
        i = 0
        while i < len(s) - 2:
            while i < len(s) - 2 and s[i] == s[i+1] == s[i+2]:
                s = s[:i+1] + s[i+2:]         
            i = i + 1
        return s
 
Java:
class Solution {
    public String makeFancyString(String s) {
        char prev = ' ';
        int equalCounter = 0;
        StringBuilder answer = new StringBuilder();
        for (char c : s.toCharArray()) {
            if (c == prev) {
                equalCounter++;
                if (equalCounter < 3) {
                    answer.append(c);
                }
            } else {
                equalCounter = 1;
                answer.append(c);
            }

            prev = c;
        }

        return answer.toString();
    }
}
Hello NNN
 
klq, cơ mà các bác cho em hỏi về complexity space với ví dụ em có một function với độ phức tạp là O(n) nhưng em gọi nó n lần ( không phải đệ quy ) thì complexity của cả thuật toán là bao nhiêu v
 
Sửa lần cuối:
Python:
class Solution:
    def isCircularSentence(self, sentence: str) -> bool:
        words = sentence.split()
        n = len(words)
        if words[-1][-1] != words[0][0]:
            return False
        for i in range(n-1):
            if words[i][-1] != words[i+1][0]:
                return False
        return True
 
PHP:
class Solution {

    /**
     * @param String $sentence
     * @return Boolean
     */
    function isCircularSentence($sentence) {
        if ($sentence[0] !== $sentence[strlen($sentence)-1]) return false;

        for ($i=0; $i<strlen($sentence); $i++) {
            if ($sentence[$i] !== ' ') continue;
            if ($sentence[$i-1] !== $sentence[$i+1]) return false;
        }

        return true;
    }
}
 
C++:
class Solution {
public:
    bool isCircularSentence(string sentence) {
        if (sentence[0] != sentence[sentence.length() - 1])
            return false;
        size_t pos = -1;
        while (string::npos != (pos = sentence.find(' ', pos + 1))) {
            if (sentence[pos - 1] != sentence[pos + 1])
                return false;
        }
        return true;
    }
};
 
Java:
class Solution {
    public boolean isCircularSentence(String sentence) {
        String[] words = sentence.split(" ");
        for(int i=0;i<words.length;i++){
            if(words[i].charAt(words[i].length()-1) != words[(i+1)%words.length].charAt(0)) return false;
        }
        return true;
    }
}
 
JavaScript:
var isCircularSentence = function(sentence) {
    const words = sentence.split(' ');
    return words.every((w, i) => w[w.length-1] === words[(i + 1) % words.length][0]);
};
 
C-like:
impl Solution {
    pub fn is_circular_sentence(sentence: String) -> bool {
        let (mut bottom, mut top) = (b' ', b' ');

        for word in sentence.split(' ') {
            let (word_bytes, len) = (word.as_bytes(), word.len());
            let (first, last) = (word_bytes[0], word_bytes[len - 1]);

            if bottom == b' ' {
                (bottom, top) = (first, last);
                continue;
            }

            if first != top {
                return false;
            }

            top = last;
        }

        bottom == top
    }
}
 
Python:
class Solution:
    def isCircularSentence(self, sentence: str) -> bool:
        n = len(sentence)

        for i in range(n):
            if sentence[i] != ' ':
                continue

            if sentence[i - 1] != sentence[i + 1]:
                return False

        return True if sentence[-1] == sentence[0] else False
 
LC 1975 Java 1liner
Java:
class Solution {
    public String makeFancyString(String s) {
        return s.length() < 3 ? s : s.replaceAll("(.)(\\1){2,}", "$1$1");
    }
}
 
LC 2490 Java 1liner
Java:
class Solution {
  public boolean isCircularSentence(String s) {
    return s.charAt(0)==s.charAt(s.length()-1) && s.matches("\\S+|.*((\\S) \\2.)+\\S+") && !s.matches(".*[ap] [bg].*");
  }
}
 
Sửa lần cuối:
C++:
class Solution {
public:
    bool isCircularSentence(string s) {
        if (s[0] != s[s.size() - 1]) return false;
        for (int i = 0; i < s.size(); i++)
            if (s[i] == ' ' && s[i-1] != s[i+1])
                return false;
        return true;
    }
};
 
Trạng thái
Không mở để trả lời thêm.

Thống kê chủ đề

Ngày tạo
_Gia_Cat_Luong_,
Người trả lời cuối
Vipluckystar,
Trả lời
17.755
Lượt xem
1.214.596
Quay lại
Lên đầu trang