[코테 풀이] Count Prefixes of a Given String

시내·2024년 7월 15일

Q_2255) Count Prefixes of a Given String

출처 : https://leetcode.com/problems/count-prefixes-of-a-given-string/

You are given a string array words and a string s, where words[i] and s comprise only of lowercase English letters.

Return the number of strings in words that are a prefix of s.

A prefix of a string is a substring that occurs at the beginning of the string. A substring is a contiguous sequence of characters within a string.

class Solution {
    public int countPrefixes(String[] words, String s) {
         int count = 0;
        for(String str : words){
            if(s.startsWith(str)) count++;
        }
        return count;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글