ROKT|Senior Software Engineer|New York

Anonymous User
802
Dec 13, 2022
Dec 16, 2022

Curious to see how others would solve this problem. It seemed simple enough to me. I solved two parts to the problem during my onsite with ROKT. Not sure if it has more parts to it or not. At the end they rejected me because they were expecting more. I am not sure what exactly they meant by more. It could be that there were more parts to the problem or the way I solved/approached the two partd were not optimal. My time and memory complexity is O(n) and O(1) respectively.

You are given two strings s1 and s2. s1 may have a wildcard character denoted by the '.' - return true if s1 and s2 are equal or else return false otherwise.
Example:
i) rokt, rokt - true
ii)r.kt, rokt - true
iii) r.k, rokt - false
iv) r..k, rokt - false

my solution:

public boolean isSame(String s1, String s2) {
	if (s1.length() != s2.length()) return false;
	
	for (int i = 0; i < s1.length(); i++) {
		char c = s1.chartAt(i);
		if (c != s2.charAt(i)) {
			if (c != '.') return false;
		}
	}
	return true;
}

2nd Part:
You are given two strings s1 and s2. The wildcard character is now denoted by a number instead of a '.' - it denotes the number of consecutive wildcard characters that would appear in s1. The number would range from 0-9.

Ex:
i) r2t, rokt - true
ii) r3t, rokt - false
iii) 1111, rokt - true
iv) r1k1, rokt - true

my solution:

public static boolean isSimilarII(String s1, String s2) {
        int i = 0, j = 0;
        for (; i < s1.length() && j < s2.length(); i++) {
            char c = s1.charAt(i);
            char d = s2.charAt(j);
            if (c != d) {
                if (Character.isDigit(c)) {
                    j += (c - '0');
                } else return false;
            } else {
                j++;
            }
        }
        return i == s1.length() && j == s2.length();
    }
Comments (1)