当前位置:网站首页>2309. The best English letters with both upper and lower case

2309. The best English letters with both upper and lower case

2022-07-05 09:06:00 Laver (nori)

Ideas : 

1. Use fixed length arrays , Store whether upper and lower case letters appear ;

2. Reverse traversal , Start with the big letters , Until you find the result ;

class Solution {
public:
    string greatestLetter(string s) {
      //  A lowercase letter 
      vector<int> auxUpper(26, 0);
      
      //  Capitalization 
      vector<int> auxLower(26, 0);

      //  Traversal string s, Record the upper and lower case letters 
      for(auto ch : s){
        if(ch >= 'A' && ch <= 'Z'){
          // upper letter
          auxUpper[(int)(ch - 'A')] = 1;
        }else{
          // lower letter
          auxLower[(int)(ch - 'a')] = 1;
        }    
      } 

      //  Traverse in reverse order to find the first letter with both upper and lower case 
      for(int i = 25; i >= 0; i--){
        if(auxUpper[i] == 1 && auxLower[i] == 1){
          return string(1, (char)(i + 'A'));
        }
      }
      
      return "";
    }
};

原网站

版权声明
本文为[Laver (nori)]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050857275323.html