inblog logo
|
stupefyee
    알고리즘문제풀기

    [JAVA 문제 풀이] 378. Valid Word

    [유효한 단어]
    Stupefyee's avatar
    Stupefyee
    Jul 15, 2025
    [JAVA 문제 풀이] 378. Valid Word
    Contents
    내가 작성한 코드다른 사람의 코드
    Valid Word - LeetCode
    Can you solve this real interview question? Valid Word - A word is considered valid if: * It contains a minimum of 3 characters. * It contains only digits (0-9), and English letters (uppercase and lowercase). * It includes at least one vowel. * It includes at least one consonant. You are given a string word. Return true if word is valid, otherwise, return false. Notes: * 'a', 'e', 'i', 'o', 'u', and their uppercases are vowels. * A consonant is an English letter that is not a vowel.   Example 1: Input: word = "234Adas" Output: true Explanation: This word satisfies the conditions. Example 2: Input: word = "b3" Output: false Explanation: The length of this word is fewer than 3, and does not have a vowel. Example 3: Input: word = "a3$e" Output: false Explanation: This word contains a '$' character and does not have a consonant.   Constraints: * 1 <= word.length <= 20 * word consists of English uppercase and lowercase letters, digits, '@', '#', and '$'.
    Valid Word - LeetCode
    https://leetcode.com/problems/valid-word/description/?envType=daily-question&envId=2025-07-15
    Valid Word - LeetCode
    notion image
    notion image
    단어는 다음과 같은 경우 유효한 것으로 간주됩니다: * 최소 3자를 포함합니다. * 숫자(0-9)와 영문(대소문자 및 소문자)만 포함되어 있습니다. * 적어도 하나의 모음이 포함되어 있습니다. * 자음이 하나 이상 포함되어 있습니다. 문자열 단어가 주어집니다. 단어가 유효하면 참으로 반환하고, 그렇지 않으면 거짓으로 반환합니다. 주의: * 'a', 'e', 'i', 'o', 'u', 대문자는 모음입니다. * 자음은 모음이 아닌 영어 문자입니다. 제약 조건: * 1 <= word.length <= 20 * 단어는 대문자와 소문자, 숫자, '@', '#', '$로 구성됩니다.
     

    내가 작성한 코드

    class Solution { public boolean isValid(String word) { // 조건문과 주의사항을 준수하는 정규식 return word.matches("^(?=.*[AEIOUaeiou])(?=.*[BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz])[A-Za-z0-9]{3,}$"); } }
     

    다른 사람의 코드

    class Solution { public boolean isValid(String word) { // 3글자 미만 걸러내기 if (word.length() < 3) { return false; } boolean hasVowel = false; // 모음이 있는지 확인 boolean hasConsonant = false; // 자음이 있는지 확인 for (char c : word.toCharArray()) { // 알파벳인지 확인 if (Character.isLetter(c)) { char ch = Character.toLowerCase(c); // 모음과 자음 확인 if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { hasVowel = true; } else { hasConsonant = true; } // 숫자 인지 확인 } else if (!Character.isDigit(c)) { return false; // 아니면 특수문자 >> false } } return hasVowel && hasConsonant; // 모음과 자음이 모두 있는지 확인 } }
     
    Share article
    Contents
    내가 작성한 코드다른 사람의 코드

    stupefyee

    RSS·Powered by Inblog