[C++] Palindrome

2024. 9. 20. 17:49ยท๐Ÿ“ Computer Science/โœ Algorithm

Palindrome

1. ๊ฐœ๋…

Palindrome์€ ๊ฑฐ๊พธ๋กœ ์ฝ์–ด๋„ ์ œ๋Œ€๋กœ ์ฝ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฌธ์žฅ์„ ๋งํ•œ๋‹ค.

 

Palindrome

 

2. ๋ฌธ์ œ

https://leetcode.com/problems/palindrome-partitioning/description/

#include <vector>
#include <string>
using namespace std;

class Solution {
public:
    vector<vector<string>> answer;
    vector<string> sub;

    vector<vector<string>> partition(string s) {
        dfs(s, 0);
        return answer;
    }

    void dfs(string s, int index) {
        if (index == s.length()) {
            answer.push_back(sub);
            return;
        }

        for (int i = index; i < s.length(); i++) {
            string substr = s.substr(index, i - index + 1);
            if (!palindrome(substr))
                continue;
            sub.push_back(substr);
            dfs(s, i + 1);
            sub.pop_back();
        }
    }

    bool palindrome(string s) {
        int size = s.length();
        for (int i = 0; i < size / 2; i++)
            if (s[i] != s[size - i - 1])
                return false;
        return true;
    }
};

 

https://leetcode.com/problems/longest-palindromic-substring/description/

#include <string>
using namespace std;

class Solution {
public:
	bool dp[1001][1001]{};

	string longestPalindrome(string s) {
		// 1๊ธ€์ž
		string answer = s.substr(0, 1);
		for (int i = 0; i < s.size(); i++)
			dp[i][i] = true;

		// 2๊ธ€์ž
		for (int i = 0; i < s.size() - 1; i++)
			if (s[i] == s[i + 1]) {
				dp[i][i + 1] = true;
				answer = s.substr(i, 2);
			}

		// 3๊ธ€์ž ์ด์ƒ
		for (int i = 3; i <= s.size(); i++)
			for (int j = 0; j + i <= s.size(); j++)
				if (dp[j + 1][j + i - 2] && s[j] == s[j + i - 1]) {
					dp[j][j + i - 1] = true;
					if (answer.size() < i)
						answer = s.substr(j, i);
				}

		return answer;
	}
};

 

https://leetcode.com/problems/shortest-palindrome/description/

#include <string>
using namespace std;

class Solution {
public:
    string shortestPalindrome(string s) 
    {
        int i = 0, n = s.size();
        for (int j = n - 1; j >= 0; --j)
            if (s[i] == s[j]) 
                ++i;

        if (i == n) 
            return s;

        string add = s.substr(i); // ์ถ”๊ฐ€ํ•ด์•ผ ํ•  ์ตœ์†Œ ๋ฌธ์ž์—ด
        reverse(add.begin(), add.end());
        return add + shortestPalindrome(s.substr(0, i)) + s.substr(i);
    }
};
์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)
'๐Ÿ“ Computer Science/โœ Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [C++] ์ด๋ถ„ ๊ทธ๋ž˜ํ”„
  • [C++] ํŽธ์ง‘ ๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜(Edit Distance Algorithm)
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ •๋ฆฌ
  • [C++] ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ(Linked List)
Blxxming
Blxxming
CS ์ง€์‹๊ณผ ๊ณต๋ถ€ํ•˜๋‹ค ๋ฐฐ์šด ๊ฒƒ, ๊ฒฝํ—˜ํ•œ ๊ฒƒ ๋“ฑ์„ ๊ธฐ๋กํ•˜๋Š” ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
  • Blxxming
    ๐Ÿ’ก๋ฒˆ๋œฉ๐Ÿ’ก
    Blxxming
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
  • ๊ณต์ง€์‚ฌํ•ญ

    • Tech Interview
    • ๐Ÿ“š Tech (246)
      • ๐Ÿ“ Computer Science (96)
        • โœ OS (12)
        • โœ Network & Web (10)
        • โœ Database (11)
        • โœ Data Structure (6)
        • โœ Algorithm (40)
        • โœ Design Pattern (9)
        • โœ Cloud Computing (3)
        • โœ (5)
      • ๐Ÿ“ Language (73)
        • โœ Language (6)
        • โœ C & C++ (11)
        • โœ C# (19)
        • โœ JAVA (37)
      • ๐Ÿ“ Game (43)
        • โœ Computer Graphics (2)
        • โœ Unity (14)
        • โœ Unreal (26)
        • โœ (1)
      • ๐Ÿ“ Book (34)
        • โœ Effective (3)
        • โœ Game Server (16)
        • โœ Clean Code (14)
        • โœ (1)
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.0
Blxxming
[C++] Palindrome
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”