ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 392. Is Subsequence
    릿코드(LEETCODE) 2020. 2. 13. 14:51
    반응형

     

    https://leetcode.com/problems/is-subsequence

     

     

    O 풀이

    class Solution {
    public:
       bool isSubsequence(string s, string t) {
          int nSSize = s.size();
          int nTSize = t.size();
          int nFindCout = 0;
          for(int i = 0; i < nTSize; i++ )
          {
             for( int j = nFindCout ; j < nSSize; j++ )
             {
                if( t[i] != s[j] )
                   break;
    
                nFindCout++;
                    i++;
             }
          }
    
          return nFindCout == nSSize;
       }
    };

     

    D 풀이

    class Solution {
    public:
        bool isSubsequence(string s, string t) {
            
            int count = 0;
            int pos = 0;
            for(int i=0; i<s.size(); i++) {
                char code = s[i];
                
                for(int j=pos; j<t.size();j++) {
                    if(code == t[j]){
                        pos = j + 1;
                        count++;
                        break;
                    }
                }
                
            }
            return s.size() == count;
        }
    };

     

    B 풀이

    class Solution {
    public:
        bool isSubsequence(string s, string t) {
          int sIdx = 0, tIdx = 0;
          while(sIdx < s.size()) {
             while (tIdx < t.size() && s[sIdx] != t[tIdx])
                tIdx++;
             if (tIdx == t.size())   // s[sIdx] 가 존재하는데 t[tIdx] 는 존재안하는 경우 false;
                return false;
             sIdx++;
             tIdx++;
          }
          return sIdx == s.size();
        }
    };
    반응형

    '릿코드(LEETCODE)' 카테고리의 다른 글

    1025. Divisor Game  (0) 2020.02.14
    53. Maximum Subarray  (0) 2020.02.13
    342. Power of Four  (0) 2020.02.13
    1290. Convert Binary Number in a Linked List to Integer  (0) 2020.02.13
    389. Find the Difference  (0) 2020.02.13

    댓글

Designed by Tistory.