[Leetcode] 14. Longest Common Prefix (C)
๋๊ฒ ์ค๋๋ง์ ํฌ์คํ ์ ์ ๋๊ฒ ๊ฐ๋ค. ํ์ฌ๋ค์ด๊ฐ๊ธฐ ์ ์๋ ํ์ฌ ์ํ ํ๋ฉด์๋ ์ด๊ฑธ ์ ์ ์งํ ์ ์์์ค ์์๋๋ฐ ์ฌ๋ฌ๋ชจ๋ก ์ด๋ ค์ด ์ผ์ด์๋ค๋๊ฑธ ์๊ฒ๋์๋ค..๐ญ
ํ์ฌ๊ฐ ์์ฆ C๋ฅผ ๋ค์ ์ฐ์ตํ๊ณ ์๊ธฐ๋๋ฌธ์ Leetcode์ ์ฌ์ด ๋ฌธ์ ๋ค์ C๋ก ํ์ด๋ณด๊ณ ์๋ค.
์ด๋ฒ์ ํผ ๋ฌธ์ ๋ 14. Longest Common Prefix๋ผ๋ ๋ฌธ์ ์๋ค.
์ ๋ ฅ์ผ๋ก strsSize๊ฐ์ ๋ฌธ์์ด์ด ๋ค์ด์ฌ๋ ํด๋น ๋ฌธ์์ด๋ค์ ๊ฐ์ฅ ๊ธด prefix๋ฅผ ์ฐพ๋ ๋ฌธ์ ์๋ค.
์ฒซ ๋ฒ์งธ ๊ตฌํ : Two pointer?Permalink
๋๋ ๊ธฐ์กด์ Longest Common Prefix์ ๋ฌธ์์ด์ ์์๋๋ก ๋น๊ตํ๋ฉฐ ๊ณตํต๋ prefix๋ฅผ ๊ฐฑ์ ํ๋ ๋ฐฉ๋ฒ์ ํํ๋ค. ์๋ฅผ ๋ค์ด input์ผ๋ก strs=[โflowerโ,โflowโ,โflightโ] ์ธ ๊ฐ์ ๋ฌธ์์ด์ด ๋ค์ด์์๋๋ฅผ ๊ฐ์ ํด๋ณด์.
๋จผ์ common์ด๋ผ๋ Longest common prefix๊ฐ์ ์ ์ฅํ ๋ฌธ์์ด์ ํ๋ ๋ง๋ค๊ณ , ํด๋น ๋ฌธ์์ด์ input์ ์ฒซ๋ฒ์จฐ ๋ฌธ์์ด๋ก ์ด๊ธฐํ ํ๋ค.
๋ฐ๋ผ์ ์ด๊ธฐ ์ํ์ common์ โflowerโ, ์ฆ str[0]์ ์ฃผ์๊ฐ์ ๊ฐ๋ฆฌํค๊ณ ์์๊ฑฐ๋ค.
์ดํ ์ด common์ ๋ค์ ๋ฌธ์์ด์ธ flow์ ํ๊ธ์์ฉ ๋น๊ตํ๋ค.
์ด ๊ฒฝ์ฐ โflowโ 4๊ธ์๊ฐ ๊ฐ์ผ๋ฏ๋ก common์ โflowโ๋ก ๊ฐฑ์ ํ๋ค.
๋ค์์ผ๋ก ๋ค์ ๋ฌธ์์ด์ธ โflightโ์ common์ ๋น๊ตํ๋ค.
์ด๊ฒฝ์ฐ โflโ๊น์ง ๊ฐ์ผ๋ฏ๋ก ๊ฒฐ๊ณผ๋ โflโ์ด ๋๋ค.
๊ตฌ์ํ์๋๋ ์ ๋ง ๊ฐ๋จํด๋ณด์ด๋ ์๊ณ ๋ฆฌ์ฆ์ด์๋๋ฐ C๋ก ๊ตฌํํ๋ ค๊ณ ํ๋๊น ๋๋ฌด ํ๋ค์๋ค..
char* longestCommonPrefix(char** strs, int strsSize) {
char* common = strs[0];
int common_last = strlen(common);
for(int i = 1; i<strsSize; i++)
{
char* compare = strs[i];
int common_current =0;
int compare_current =0;
while(common_current<common_last && compare_current <common_last)
{
if(common[common_current]==compare[compare_current])
{
common_current++;
compare_current++;
}else{
if(common_last>common_current)
common_last = common_current;
break;
}
}
}
char* res = (char *)malloc(sizeof(char) * (common_last+1));
strncpy(res,common,common_last);
res[common_last]='\0';
return res;
}
๊ฒฐ๊ณผ๋ ์๋์ ๊ฐ๋ค.
c๊ฐ ๋น ๋ฅด๊ธด ๋น ๋ฅด๋ค.
์๊ฐ ๋ณต์ก๋๋ O(n*m)์ด์๋๋ฐ ๊ฐ์ ํ ๋ฐฉ๋ฒ์ด ์์๊ฒ ๊ฐ๊ธฐ๋ ํ๊ณ .
Memory๊ฐ ์์ธ๋ก ๋ ์ค์ผ ์ ์์๋๋ณด๋ค.
๋ ๋ฒ์จฐ ์๋ : ๊ฐ์ Permalink
๋ต์ง๋ฅผ ๋ณด๋๊น ์๊ณ ๋ฆฌ์ฆ์ ๋์ถฉ ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ์ด์๋๊ฒ ๊ฐ๋ค.
ํ, ๊ทผ๋ฐ ๋ต์ ์ฝ๋๋ฅผ ๋ณด๋๊น ๋ด๊ฐ ๋์น ๋ถ๋ถ๋ ์๊ณ , ๋ฏธ์ฒ ์ด๋ ๊ฒ ๊น์ง ์๊ฐํ์ง ๋ชปํ๋ ๋ถ๋ถ์ด ์๋ค.
- strstrํจ์์ฌ์ฉ
- ์ ๋ ฅ ๋ฌธ์์ด ๋ฐฐ์ด์ ํ์ฉํด ๋ฆฌํด๊ฐ ๋ฐํ
- (minor) ์ ๋ ฅ ๊ฒ์ฆ
strstr(st1,st2)Permalink
#include <string.h>
char *strstr(const char *string1, const char *string2);
- string1์์ string2์ ์ฒซ๋ฒ์งธ ์ผ์นํ๋ ๋ฌธ์์ด์ ์ฐพ๋๋ค.
- ์ผ์นํ๋ ๋ฌธ์์ด์ด ์๋ค๋ฉด ํด๋น ์์น์ ํฌ์ธํฐ๋ฅผ ๋ฐํํ๋ค.
- ๊ทธ๋ ์ง ์๋ค๋ฉด NULL์ ๋ฐํํ๋ค.
+) ๊ฐ์๊ธฐ ๊ถ๊ธํด์ ธ์ string2์๋ฆฌ์ โโ์ ๋ฃ์ด์ ์๋ํด๋ณด์๋ค.
int main() {
char* a = "hello";
char* b = "";
printf("%c",*strstr(a,b)+1); //output : h
}
ํ .. ์์ธ๋ก a์ ์ฒซ๋ฒ์งธ ์ฃผ์๊ฐ์ด ๋ฐํ๋์๋ค.
๊ถ๊ธํด์ ์๊ทธ๋ฐ๊ฐ ํ๊ณ ๋ดค๋๋ฐ ๋ด๋ถ์ ์ผ๋ก b๊ฐ ๊ณต๋ฐฑ ๋ฌธ์๋ฉด ๋ฐ๋ก string1์ ๋ฐํํ๊ฒ ๋์ด์๋ค. (stackoverflow)
ํ์ฌ๊ฐ ์ด๊ฑธ๋ก ๋ค์ ์ฝ๋๋ฅผ ์์ฑํด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
char* longestCommonPrefix(char** strs, int strsSize) {
if(strsSize==0){
return 0;
}
char* common = strs[0];
for(int i =0; i<strsSize; i++){
while(*common){
if(strstr(strs[i],common)!=strs[i]){
common[strlen(common)-1]='\0';
}else{
break;
}
}
}
return common;
}
์ดด..ใ ใ ใ ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ ๋์ด๋ฒ๋ ธ๋ค..
์๋ ๋ด ์ฝ๋๋ ์ ์ ํ ์์ด์ ์๋ ๋ด ์ฝ๋๋ฒ ์ด์ค๋ก ๊ฐ์ ์ ํ๋๊ฒ ์ฑ๋ฅ ์ธก๋ฉด์์ ๋ ๋์๊ฒ ๊ฐ์์ ์ธ๋ฒ์งธ ์๋๋ฅผ ํ๋ค.
์ธ๋ฒ์งธ ์๋ : ์ฒ์ ์ฝ๋ ์ต์ ํPermalink
char* longestCommonPrefix(char** strs, int strsSize) {
char* common = strs[0];
int common_last = strlen(common);
for(int i = 1; i<strsSize; i++)
{
char* compare = strs[i];
int common_current =0;
int compare_current =0;
while(common_current<common_last && compare_current <common_last)
{
if(common[common_current]==compare[compare_current])
{
common_current++;
compare_current++;
}else{
if(common_last>common_current)
common_last = common_current;
break;
}
}
}
common[common_last] = '\0';
return common;
}
๋ง์ง๋ง์ res๋ฅผ ๋์ ํ ๋นํ๋ ๋ถ๋ถ๋ง ๋ณ๊ฒฝํ๋ค.
๊ทผ๋ฐ ๊ฒฐ๊ณผ๊ฐ ๋๋ฒ์งธ ์๋์ ๋น์ทํ๊ฒ ๋์๋ค. ์๋ง ๋์ ํ ๋น์ ํฌ๊ฒ ์ํฅ์ ๋ฏธ์น๋ ๋ถ๋ถ์ด ์๋์๋๋ณด๋ค.
๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ์๋ค๊ฐ ์ด๋ป๊ฒ ๊ณ์ฐ์ ํ๋์ง ์ ๋ชจ๋ฅด๊ฒ ๋๋ฐ ๋์ค์ ์๊ฐ๋๋ฉด ํ๋ฒ ์ฐพ์๋ด๋ ์ข์๊ฒ ๊ฐ๊ธดํ๋ค.
๊ฒฐ๋ก Permalink
๊ฒฐ๋ก ์ ๋๋ฒ์งธ๊ฐ ๊ฐ์ฅ ๋์ ์ฝ๋๊ฐ๋ค. ์ฒซ๋ฒ์งธ๋ณด๋ค ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ๋๊ธดํ์ง๋ง ๊ฐ๋ ์ฑ๊ณผ ์ฝ๋ ์๊ฐ ํจ์ฌ ์ค์๋ค.
์ค๋ ๋ฐฐ์ด๊ฑด ์๋์ ๊ฐ๋ค.
- leetcode์์ ๊ฒฐ๊ณผ๋ฐํ์ input๋ฐฐ์ด์ ํ์ฉํ ์๋ ์๋ค (๊ทผ๋ฐ ์ ์ง๋ชจ๋ฅด๊ฒ ์ด๋ฒ์ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ๋์๋ค.๐ค๐ค๐ค)
- strstr๋ก ๋ฌธ์์ด์ ํฌํจ๋ ๋ฌธ์์ด์ ํ์ํ ์ ์๋ค.
- ๋ฌธ์์ด ๋๋ถ๋ถ์ dropํ๊ณ ์ถ์๋๋ strncpy๋ง๊ณ ๋ ๊ทธ๋ฅ ๋๋ด๊ณ ์ถ์ ๋ถ๋ถ์ โ\0โ์ ๋ฃ๋๊ฒ๋ ๋ฐฉ๋ฒ์ด๋ค.(๋์ค์ ๋ ๋ฐฉ๋ฒ์ ๋ํด์๋ ๋น๊ต๋ฅผ ์ข ํด๋ด์ผ๊ฒ ๋ค.)
๋๊ธ๋จ๊ธฐ๊ธฐ