Google | Phone Screen | Isomorphic Strings & Rhyme Schemes
Anonymous User
3278

Position: SWE New Grad - 2019.
Date: November, 2018.

Question:
Variant of https://leetcode.com/problems/isomorphic-strings
Suppose there are two rhyming schemes, tell if these two schemes are equivalent or not.

Example 1:

Input: s1 = "AABBCC", s2 = "XXYYXX"
Output: false
Explanation:
They are not equivalent, as A -> X, B -> Y, but C does not have a different letter, these rhyming schemes are different.

Example 2:

Input: s1 = "ABCABC", s2 = "XYZXYZ"
Output: true

Follow-up:
Rhyme Schemes

I tried this question after the interview using a tree. But my solution goes into stack overflow for poems with more than 6 lines.

Here's my solution:
import java.util.*;
public class allRhymingSchemes {
	
	static class TreeNode{
		
		StringBuilder word;
		List<TreeNode> kids;
		
		public TreeNode(StringBuilder word) {
			this.word = word;
			kids = new LinkedList<TreeNode>();
		}
		
	}
	
	
	public static List<String> generateAllRhymingSchemes(int n){
		List<String> result = new LinkedList<String>();
	
		 
		Queue<TreeNode> queue = new LinkedList<TreeNode>();
		StringBuilder init = new StringBuilder();
		init.append('A');
		
		TreeNode root = new TreeNode(init);
		//System.out.println(root.word.toString());
		queue.add(root);
		while(!queue.isEmpty()) {
			
			TreeNode line = queue.poll();
			//System.out.println("Popping " + line.word.toString());
			if(line.word.length() == n) {
				String answer = new String(line.word);
				result.add(answer);
			}
			else {
				//Now for this, create either from one of the chars present in the parent
				//or take a completely different route, which is last char + 1 (lexicographically).
				StringBuilder nextStep = line.word;
				HashSet<Character> set = new HashSet<Character>();
				for(int i = 0; i < nextStep.length(); i++) {
					//Rhyme with a previous line if we haven't already
					char ch = nextStep.charAt(i);
					if(!set.contains(ch)) {
						//System.out.println("Appending " + ch + " to " + nextStep.toString());
						StringBuilder sb = new StringBuilder(nextStep);
						sb.append(ch);
						//This is our new rhyming scheme
						TreeNode node = new TreeNode(sb);
						//System.out.println("New rhyming scheme is " + sb.toString());
						//Add as a children to our tree
						line.kids.add(node);
						//Add this to the queue
						queue.add(node);
						set.add(ch);
					}
				}
				//After rhyming with every line rhymed before
				//Our third option is to create a completely new line
				
				StringBuilder sb = new StringBuilder(nextStep);
				char newOne = (char)(nextStep.charAt(nextStep.length() - 1) + 1);
				sb.append(newOne);
				TreeNode node = new TreeNode(sb);
				//System.out.println("Not rhyming with any line before, creating rhyming scheme " + sb.toString());
				line.kids.add(node);
				queue.add(node);
			}

		}
		return result;
	}
	
	public static void main(String[] args) {
		List<String> answer = generateAllRhymingSchemes(5);
		System.out.println(Arrays.toString(answer.toArray()));
	}

}
Comments (9)