Skip to content

Commit 9094f47

Browse files
author
Iurii Dziuban
committed
updated solutions/explanation/details to all GooglePhoneCall task
1 parent 0a53d31 commit 9094f47

File tree

1 file changed

+58
-28
lines changed

1 file changed

+58
-28
lines changed

src/main/java/iurii/job/interview/google/GooglePhoneCall.java

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import java.util.Map;
66
import java.util.Set;
77

8-
public class GooglePhoneCall {
8+
/**
9+
Task that was given on Google Phone interview:
910
10-
/*
1111
There are two strings with same size.
1212
containing lower case latin (a-z)
1313
@@ -33,37 +33,45 @@ containing lower case latin (a-z)
3333
all same letter occurrences on the same positions.
3434
lengths of two strings should be the same.
3535
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-
5936
Leetcode example : https://leetcode.com/discuss/interview-question/340493/Google-or-Onsite-or-String-Conversion
6037
6138
Take care of homomorphic /isomorphic changes.
6239
6340
Leetcode : https://leetcode.com/problems/string-transforms-into-another-string/
6441
*/
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
6553
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+
*/
6671
// 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.
6775
public boolean canTransformNotBest(String a, String b) {
6876
if (a.length() != b.length()) {
6977
return false;
@@ -99,7 +107,8 @@ public boolean canTransformNotBest(String a, String b) {
99107
* Space Complexity - O(1) = 26 * 1 (number of letters) for Map key-value
100108
* Time Complexity - O(N) - to check the whole string
101109
*
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?
103112
*
104113
* @param a - start string
105114
* @param b - end string
@@ -121,8 +130,29 @@ public boolean canTransformBetter(String a, String b) {
121130
return true;
122131
}
123132

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+
*/
126156
public boolean canConvert(String str1, String str2) {
127157
if (str1.length() != str2.length()) {
128158
return false;

0 commit comments

Comments
 (0)