본문 바로가기

C

c++ leetcode 680번 valid palindrome II

code >>

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Solution {
public:
    bool validPalindrome(string s) {        
        int strlen = (int)s.size();
        int i = 0;
        int j = strlen - 1;
        if (j == 0)
            return true;
 
        while (j >= 0 && i < strlen)
        {
 
            if ((s[j] != s[i]))
            {
                string temp1 = s;
                temp1.erase(i,1);
                string temp2 = s;
                temp2.erase(j, 1);
 
                bool result1 = this->deletedPalindrome(temp1);
                bool result2 = this->deletedPalindrome(temp2);
                if ((result1) || (result2))
                    return true;
                else
                    return false;
            }        
            else
            {
                j--;
                i++;
            }
        }
        
        return true;
    }
    
    bool deletedPalindrome(string s)
    {
        int strlen = (int)s.size();
        int i = 0;
        int j = strlen - 1;
        if (j == 0)
            return true;
 
        while (j >= 0 && i < strlen)
        {
 
            if ((s[j] != s[i]))
            {
                return false;
            }
 
            else
            {
                j--;
                i++;
            }
        }
        return true;
    }
};