Skip to content

Latest commit

 

History

History
26 lines (22 loc) · 591 Bytes

File metadata and controls

26 lines (22 loc) · 591 Bytes

LeetCode Records - Question 1003 Check If Word Is Valid After Substitutions

Attempt 1: Use replace() to remove "abc" strings

class Solution {
    public boolean isValid(String s) {
        int len = s.length();
        if (len % 3 != 0) {
            return false;
        }

        int prevLen;
        do {
            prevLen = len;
            s = s.replace("abc", "");
            len = s.length();
        } while (prevLen != len);

        return s.length() == 0;
    }
}
  • Runtime: 4 ms (Beats: 96.98%)
  • Memory: 43.98 MB (Beats: 99.70%)