본문 바로가기

Algorithm/String

(91)
(C++) - LeetCode (easy) 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/description/ find함수 사용과 split을 구현한 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 📔 풀이과정 1. stringstream을 이용, 구분자를 공백 한 글자로 split함수를 구현해 수행한 결과를 vector s에 저장해줍니다. 2. s에 대해 원소를 순회하며 접두어로 searchWord가 나왔는지 find함수의 결과로 확인합니다. 2-1. 찾지 못했다면 string::npos, 찾았다면 string::size_type(unsigned long long)을 반환하므로 0인지 확인하면 됩니다. 2-2. 찾았다면 i+1을 ..
(C++) - LeetCode (easy) 1417. Reformat The String https://leetcode.com/problems/reformat-the-string/description/ 문자열을 다뤄본 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 숫자와 alphabat 문자의 개수 및 내용을 각각 세기 위해 stack digits, alphas를 선언해 저장해줍니다. 📔 풀이과정 1. 비둘기집의 원리에 의해 digit과 alphabat이 번갈아 나오려면 문자 개수의 차이가 1이하여야 가능합니다. 2개 이상 차이 난다면 어떤 방법으로도 연속적으로 두 문자가 나오게 되므로 번갈아 배치할 수 없습니다. 따라서 이 경우에는 ""를 반환합니다.2. 문자의 차이가 1개라면 더 많은 문자가 양 끝에 배치해야 성공합니다. 따라서 digits와 alphas 중 더 많은 문자를 보유한 sta..
(C++) - LeetCode (easy) 1309. Decrypt String from Alphabet to Integer Mapping https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/description/ LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문자열을 다루는 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 반환할 복호화된 문자열 decryptedString을 선언해줍니다. 📔 풀이과정 s의 원..
(C++) - LeetCode (easy) 1160. Find Words That Can Be Formed by Characters https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/description/ Find Words That Can Be Formed by Characters - LeetCode Can you solve this real interview question? Find Words That Can Be Formed by Characters - You are given an array of strings words and a string chars. A string is good if it can be formed by characters from chars (each character can only be used once). Retu..
(C++) - LeetCode (easy) 1108. Defanging an IP Address https://leetcode.com/problems/defanging-an-ip-address/description/ Defanging an IP Address - LeetCode Can you solve this real interview question? Defanging an IP Address - Given a valid (IPv4) IP address, return a defanged version of that IP address. A defanged IP address replaces every period "." with "[.]". Example 1: Input: address = "1.1.1.1" Outp leetcode.com 문자열을 다뤄보는 문제였습니다. 📕 풀이방법 📔 입력 및..
(C++) - LeetCode (easy) 1078. Occurrences After Bigram https://leetcode.com/problems/occurrences-after-bigram/description/ Occurrences After Bigram - LeetCode Can you solve this real interview question? Occurrences After Bigram - Given two strings first and second, consider occurrences in some text of the form "first second third", where second comes immediately after first, and third comes immediately after sec leetcode.com 문자열을 다루는 문제였습니다. 📕 풀이방법 ..
(C++) - LeetCode (easy) 1002. Find Common Characters https://leetcode.com/problems/find-common-characters/ LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문자열 다루는 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 1. alphabat별 전체 words의 최소 빈도를 저장할 vector minFreq를 선언 후 각 값을 INT_MAX로 초기화해줍니다. 2. 정답 변수 ..
(C++, Rust) - LeetCode (easy) 944. Delete Columns to Make Sorted https://leetcode.com/problems/delete-columns-to-make-sorted/description/ strs[j+1][i]) { isNotLexicographically = true; break; } } if (isNotLexicographically) columnsToDelete++; } return columnsToDelete; } }; 📔 Rust impl Solution { pub fn min_deletion_size(strs: Vec) -> i32 { let mut columns_to_delete = 0; for i in 0..strs[0].len() { for j in 0..strs.len() - 1 { if strs[j].as_bytes()[i] > strs[j..