Post

문자열 내 p와 y의 개수

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 public class NumberOfPY {
    public boolean howManyPY(String str) {
        String lowerString = str.toLowerCase();
        char[] charArray = lowerString.toCharArray();

        int numP=0;
        int numY=0;

        for (char ch : charArray) {

            if(ch == 'p'){
                numP++;
            }
            if(ch == 'y'){
                numY++;
            }
        }
        if (numP == numY | numP==0 | numY==0){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        NumberOfPY py= new NumberOfPY();
        System.out.println(py.howManyPY("Pyy"));
    }

}
This post is licensed under CC BY 4.0 by the author.