LeetCode 76. Minimum Window Substring

심규환·2022년 3월 23일
0

Algorithm

목록 보기
1/5

문제 링크 : https://leetcode.com/problems/minimum-window-substring/

문제 :
Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "".

The testcases will be generated such that the answer is unique.

A substring is a contiguous sequence of characters within the string.

개인 풀이 :
런타임에러가 발생하여 이를 찾아야 한다.

class Solution {
private : 
   string result = "";
   int min = 999999;
public:
   // 첫 번째 값 찾기
    int searchFirst(string s, string t);
    
   // 마지막 값 찾기
    int searchLast(string s, string t);
    
   // target 값 있는지 확인 
    bool isInside(string s, char target);
    
    // 처음 값 지우기 
    string eraseFromBegin(string s, char target);
    
   // 마지막 값 지우 기 
    string eraseFromEnd(string s, char target);
    
    // 문자열이 있는지 확인 
    bool checkInside(string s, string t);
    
    // char 값이 있는지 확인  
    bool checkInsideChar(string s, char c);
    
    // 전체 순환 전 체크 함수  
    void beforeCalculate(int start, string s, string t );
    
    // 전체 순환  
    void calculate(int start, string s, string t);
    
    string minWindow(string s, string t) {
      // 맞는 값이 없으면 "" 리턴  
      if(!checkInside(s, t)) return result;
      
      int first = searchFirst(s, t);
      
      while(checkInsideChar(s , s[first])){
         first = searchFirst(s, t);
        beforeCalculate(first, s, t);
         s = eraseFromBegin(s, s[first]);
     }
      return result;
    }
};
void Solution::beforeCalculate(int start, string s, string t){
   while(s.size() > 0){
      if(!checkInside(s, t))break;
      string tmp = t;
      
      tmp = eraseFromBegin(tmp, s[start]);
       int last = searchLast(s,tmp);
       if(last == -1){
          result = s[start];
          break;
      }   
      
      calculate(start, s, tmp);
      
      s = s.substr(start+1, s.size()-start+2);
      start = searchFirst(s, t);
   }
}

void Solution::calculate(int start, string s, string t ){
   int last = searchLast(s, t);
   while(checkInsideChar(s , s[last])){
      string sub = s.substr(start, last-start+1);
      if(checkInside(sub, t)){
           if((int)sub.size() < min){
              result = sub;
              min = sub.size();
           }
        }
       s = eraseFromEnd(s, s[last]);
       if(s.size() == 1) break;
       last = searchLast(s,t);
   }
}

int Solution::searchFirst(string s, string t){
   int first = 999999;
   for(int i=0; i<(int)t.size(); i++){
      int tmp = s.find(t[i]);
      if(tmp == (int)string::npos) continue;
      if(tmp < first) first = tmp;
   }
   return first;
}

int Solution::searchLast(string s, string t){
   int last = -1;
   for(int i=0; i<(int)t.size(); i++){
      int tmp = s.rfind(t[i]);
      if(tmp == (int)string::npos) continue;
      if(tmp > last) last = tmp;
   }
   return last;
}

bool Solution::isInside(string s, char target){
   if(s.find(target) == string::npos) return false;
   else return true;    
}

bool Solution::checkInsideChar(string s, char c){
   if( s.find(c) == string::npos ) return false;
   else return true;
}

string Solution::eraseFromBegin(string s, char target){
   s.erase(find(s.begin(), s.end(), target));
   return s;    
}

string Solution::eraseFromEnd(string s, char target){
   s.erase(s.rfind(target));
   return s;
}

bool Solution::checkInside(string s, string t){
   for(int i=0; i<(int)t.size(); i++){
      if(isInside(s, t[i])){
         s = eraseFromBegin(s, t[i]);
      }else return false;
   }
   return true;    
}
  • DEV c++, 와 기타 웹 컴파일러 등 세곳에서는 정상 작동하지만 아래와 같은 런타임 에러가 발생한다.
    리눅스 환경인거 같은데.

basic_string.h 링크

basic_string.h
http://people.redhat.com/bhubbard/cov-html-int-2019-10-09/1/86basic_string.h.html

에러가 발생되는 곳은 아래의 1070번의 return 값에서 오버플로우가 발생하는데.

1062 	      reference
1063 	      operator[](size_type __pos)
1064 	      {
1065 	        // Allow pos == size() both in C++98 mode, as v3 extension,
1066 		// and in C++11 mode.
1067 		__glibcxx_assert(__pos <= size());
1068 	        // In pedantic mode be strict in C++98 mode.
1069 		_GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
1070 		return _M_data()[__pos];
1071 	      }

원인 코드 찾는 중

profile
장생농씬가?

0개의 댓글