###### tags: `Week_1`, `String` # String ## [205. Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings) harry: ```ruby= # @param {String} s # @param {String} t # @return {Boolean} def is_isomorphic(s, t) lookup = {} dict = Set.new s.size.times do |i| c = lookup[s[i]] if c.nil? if dict.include?(t[i]) # s[i] not seen, but t[i] already seen return false else lookup[s[i]] = t[i] dict.add(t[i]) end elsif c!=t[i] return false end end true end ``` Sam: ```python= class Solution: def isIsomorphic(self, s: str, t: str) -> bool: dict_s = dict() s_list = [] dict_t = dict() t_list = [] for i in range(len(s)): if s[i] not in dict_s: dict_s[s[i]] = [i] else: dict_s[s[i]].append(i) if t[i] not in dict_t: dict_t[t[i]] = [i] else: dict_t[t[i]].append(i) for _, ar in dict_s.items(): s_list.append(ar) for _, ar in dict_t.items(): t_list.append(ar) return s_list == t_list ``` beny: ```csharp= public bool IsIsomorphic(string s, string t) { HashSet<char> tHashSet = new HashSet<char>(); Dictionary<char,char> sDictionary = new Dictionary<char, char>(); for (int i = 0; i < s.Length; i++) { if (sDictionary.ContainsKey(s[i])) { if (sDictionary[s[i]] != t[i]) { return false; } } else { if (!tHashSet.Contains(t[i])) { sDictionary.Add(s[i], t[i]); tHashSet.Add(t[i]); } else { return false; } } } return true; } ``` ## [392. Is Subsequence](https://leetcode.com/problems/is-subsequence) harry: ```ruby= # @param {String} s # @param {String} t # @return {Boolean} def is_subsequence(s, t) checker = 0 s.each_char do |c| while c != t[checker] return false if checker >= t.size - 1 checker += 1 end checker += 1 end true end ``` harry(2nd try): ```ruby= # @param {String} s # @param {String} t # @return {Boolean} def is_subsequence(s, t) return true if s == "" p_s = 0 p_t = 0 while p_t < t.size && p_s < s.size if s[p_s] == t[p_t] p_s += 1 end p_t += 1 end p_s > s.size - 1 end ``` Sam: ```python= class Solution: def isSubsequence(self, s: str, t: str) -> bool: match_index = 0 i = 0 if len(t) < len(s): return False if len(s) == 0: return True while match_index < len(s) and i < len(t): if s[match_index] == t[i]: match_index += 1 i += 1 if match_index == len(s): return True return False ``` beny: ```csharp= public bool IsSubsequence(string s, string t) { if(s.Length > t.Length){ return false; } int next = 0; for (int j = 0; j < s.Length; j++){ if (next == t.Length) { return false; } for (int i = next; i < t.Length; i++) { next = i + 1; if (s[j] == t[i]) { break; }else if(next== t.Length){ return false; } } } return true; } ``` beny(2nd try): ```csharp= public class Solution { public bool IsSubsequence(string s, string t) { if(s.Length == 0) { return true; } else if(s.Length > t.Length) { return false; } int index = 0; for(int i = 0;i < t.Length; i++) { if(s[index] == t[i]){ index ++; } if(index == s.Length){ return true; } } return false; } } ``` ## [290. Word Pattern](https://leetcode.com/problems/word-pattern/description/) beny: ```csharp= public class Solution { public bool WordPattern(string pattern, string s) { Dictionary<Char,string> dic = new Dictionary<Char,string>(); string[] array = s.Split(' '); if(array.Length != pattern.Length) { return false; } for(int i = 0; i < pattern.Length; i ++) { if(dic.ContainsKey(pattern[i])) { if(array[i] != dic[pattern[i]]) { return false; } } else { if(!dic.ContainsValue(array[i])) { dic.Add(pattern[i],array[i]); } else { return false; } } } return true; } } ```