[코테 풀이] Maximum Value of a String in an Array

시내·2024년 7월 15일
0

Q_2496) Maximum Value of a String in an Array

출처 : https://leetcode.com/problems/maximum-v

The value of an alphanumeric string can be defined as:

  • The numeric representation of the string in base 10, if it comprises of digits only.

  • The length of the string, otherwise.

Given an array strs of alphanumeric strings, return the maximum value of any string in strs.

class Solution {
    public int maximumValue(String[] strs) {
        int max = 0;
        for (int s = 0; s < strs.length; s++) {
            if(max < chk(strs[s])){
                max = chk(strs[s]);
            }
        }
        return max;
    }

    public int chk(String s) {
        boolean isDigit = Character.isDigit(s.charAt(0));
        for (int q = 1; q < s.length(); q++) {
            if (isDigit) { //number
                if (Character.isLetter(s.charAt(q))) {
                    return s.length();
                }
            } else { //letter
                if (Character.isDigit(s.charAt(q))) {
                    return s.length();
                }
            }
        }
        if (isDigit) { //number
            return Integer.parseInt(s);
        } else { //string
            return s.length();
        }
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글