728x90
https://leetcode.com/problems/isomorphic-strings/
Isomorphic Strings - LeetCode
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
paper title
01023 01023
이와 같이 알파벳의 위치와 개수가 동일한지 아닌지 체크하면 된다.
나는 위의 예제처럼 숫자를 넣어줬다. 각 인덱스를 보고 이전에 있던 거면 그때 그 숫자를 넣어주고 새로운 거면 +1 한 값을 넣어줬다.
그런데 여기서 문제는 바로
"abcdefghijklmnopqrstuvwxyzva", "abcdefghijklmnopqrstuvwxyzck"
이 친구다.
이렇게 풀 줄 알았나보다.. 이걸 해보면
012345678910111213141516171819202122232425210
012345678910111213141516171819202122232425210
이렇게 동일하게 나온다. 왜냐? v = 21 a = 0, c = 2 k = 10 이기 때문에.. 붙여놓으면 동일하다
그래서 이걸 어떻게 하면 좋을까 하다가 동일한 애가 나오면 두 번 붙여주자..!
사실 이렇게 하면 값이 늘어나기 때문에 메모리상으로 좋지 않을거 같긴 한데 일단 해봤다.
됐다..^^
bool isIsomorphic(string s, string t) {
bool answer = false;
string s1 = "0", t1 = "0";
int index = 1;
for(int i=1; i<s.size(); i++){
char c = s[i];
int flag = -1;
for(int j=0; j<i; j++){
if(c == s[j]) {
flag = j;
break;
}
}
if(flag != -1) s1 += (to_string(flag)+to_string(flag));
else s1 += to_string(index++);
}
index = 1;
for(int i=1; i<t.size(); i++){
char c = t[i];
int flag = -1;
for(int j=0; j<i; j++){
if(c == t[j]) {
flag = j;
break;
}
}
if(flag != -1) t1 += (to_string(flag)+to_string(flag));
else t1 += to_string(index++);
}
if(s1 == t1) return true;
return answer;
}
728x90
'알고리즘 > LeetCode' 카테고리의 다른 글
[LeetCode/easy] Word Pattern (0) | 2022.08.12 |
---|---|
[LeetCode/easy] Valid Anagram (0) | 2022.08.12 |
[LeetCode/easy] Valid Palindrome (0) | 2022.08.12 |
[LeetCode/easy] Add Binary (0) | 2022.08.12 |
[LeetCode/easy] Length of Last Word (0) | 2022.08.12 |