題目地址:
https://www.lintcode.com/problem/attendance-judgment/description
給定一個字符串,裏面只含有三種字母(也可以缺其中的某個或者某幾個)。問是否有連續的個出現或者是否出現了多於等於兩次,如果是則返回true,否則返回false。代碼如下:
public class Solution {
/**
* @param record: Attendance record.
* @return: If the student should be punished return true, else return false.
*/
public boolean judge(String record) {
// Write your code here.
if (record.contains("LLL")) {
return true;
}
int D = 0;
for (int i = 0; i < record.length(); i++) {
D += record.charAt(i) == 'D' ? 1 : 0;
}
return D >= 2;
}
}
時間複雜度,空間。