5
5
import java .util .Map ;
6
6
import java .util .Set ;
7
7
8
- public class GooglePhoneCall {
8
+ /**
9
+ Task that was given on Google Phone interview:
9
10
10
- /*
11
11
There are two strings with same size.
12
12
containing lower case latin (a-z)
13
13
@@ -33,37 +33,45 @@ containing lower case latin (a-z)
33
33
all same letter occurrences on the same positions.
34
34
lengths of two strings should be the same.
35
35
36
- Collect all the indexes of occurred letter
37
-
38
- Map<Character, Set<Integer>>
39
- a -> 0, 3
40
- b -> 1
41
- c -> 2
42
-
43
- Then go through the keySet of Map and for each value in set check same letter in the resulting string
44
-
45
- Time complexity : O(N)
46
- Memory Complexity : O(N) - because tracking all of the cases.
47
-
48
-
49
- Map solution is nice, but what if we have all 26 characters from a to z
50
- and second string just shifted by 1 position?
51
-
52
- abc...z
53
- zabc...y
54
-
55
- we can not transform first string into second cause will get into collapsing...
56
- Can we use intermediate operations - yes.
57
- There should be additional check if there is no cycle
58
-
59
36
Leetcode example : https://leetcode.com/discuss/interview-question/340493/Google-or-Onsite-or-String-Conversion
60
37
61
38
Take care of homomorphic /isomorphic changes.
62
39
63
40
Leetcode : https://leetcode.com/problems/string-transforms-into-another-string/
64
41
*/
42
+ public class GooglePhoneCall {
43
+
44
+ /**
45
+ * Collect all the indexes of occurred letter
46
+ *
47
+ * Map<Character, Set<Integer>>
48
+ * a -> 0, 3
49
+ * b -> 1
50
+ * c -> 2
51
+ *
52
+ * Then go through the keySet of Map and for each value in set check same letter in the resulting string
65
53
54
+ * Map solution is nice, but what if we have all 26 characters from a to z
55
+ * and second string just shifted by 1 position?
56
+ *
57
+ * abc...z
58
+ * zabc...y
59
+ *
60
+ * we can not transform first string into second cause will get into collapsing...
61
+ * Can we use intermediate operations - yes.
62
+ * There should be additional check if there is no cycle
63
+ *
64
+ * This is kind of graph cycle problem.
65
+ * Time complexity : O(N)
66
+ * Memory Complexity : O(N) - because tracking all the cases.
67
+ * @param a first string
68
+ * @param b second string
69
+ * @return true if it is possible to change a to b
70
+ */
66
71
// first attempt
72
+ // no need to have Map<Character, Set<Integer>> it is enough to have Map<Character, Character>
73
+ // and check if already exists mapping is same.
74
+ // what if all characters are used? -> even if mapping is correct there will be a cycle - not solved here.
67
75
public boolean canTransformNotBest (String a , String b ) {
68
76
if (a .length () != b .length ()) {
69
77
return false ;
@@ -99,7 +107,8 @@ public boolean canTransformNotBest(String a, String b) {
99
107
* Space Complexity - O(1) = 26 * 1 (number of letters) for Map key-value
100
108
* Time Complexity - O(N) - to check the whole string
101
109
*
102
- * Note! Not proper solution
110
+ * Note! Not proper solution what is there is a cycle
111
+ * and can we use temporal `letter` to avoid cycles?
103
112
*
104
113
* @param a - start string
105
114
* @param b - end string
@@ -121,8 +130,29 @@ public boolean canTransformBetter(String a, String b) {
121
130
return true ;
122
131
}
123
132
124
- // Proper Leetcode Solution
125
- // Leetcode : https://leetcode.com/problems/string-transforms-into-another-string/
133
+ /**
134
+ * Proper Leetcode Solution
135
+ * Leetcode : https://leetcode.com/problems/string-transforms-into-another-string/
136
+ *
137
+ * Checks if there is a cycle and uses spare temporal `letter` to temporal conversion
138
+ * by set.size() < 26
139
+ *
140
+ * if all the characters are used from second word (set.size() == 26):
141
+ * 1. there is no spare letter for conversion
142
+ * 2. if str1 != str2 - there will be a cycle.
143
+ *
144
+ * If there is a spare letter it is always possible to break a cycle.
145
+ *
146
+ * But if two same letters in first string correspond to two different letters in second string
147
+ * conversion is not possible. This is invariant that does not change.
148
+ *
149
+ * This is a shortcut cycle check solution here.
150
+ * In general, it is better to go via each letter (26 total) and go via mapping and see there is no cycle.
151
+ *
152
+ * @param str1 first string
153
+ * @param str2 second string
154
+ * @return true if possible to convert str1 -> str2
155
+ */
126
156
public boolean canConvert (String str1 , String str2 ) {
127
157
if (str1 .length () != str2 .length ()) {
128
158
return false ;
0 commit comments