is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.
CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.
Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.
Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.
package com.company;
- artem.shustov August 31, 2020public class Main {
public static void main(String[] args) {
String s = "abc(c))";
//O(n)
System.out.println(parseStr(s));
}
static String parseStr(String s) {
//O(n)
if (!stringIsValid(s)) {
return "INVALID";
}
String result = null;
char[] chars = s.toCharArray();
//O(C)
for (int i = 0; i < chars.length; i++) {
int mirroredIndex = chars.length - i;
String validatedString = s.substring(i, mirroredIndex);
//O(n)
boolean valid = checkStringFullyInParethesis(validatedString);
if (!valid) {
result = s.substring(i, mirroredIndex);
break;
}
}
return result;
}
static boolean checkStringFullyInParethesis(String s) {
if (s.indexOf('(') == 0) {
int firstCloseParenthesisIndex = s.indexOf(')');
if (firstCloseParenthesisIndex > 0) {
for (var i = firstCloseParenthesisIndex + 1; i < s.length(); i++) {
char c = s.charAt(i);
if (c != ')' && c != '(') {
return false;
}
}
return true;
}
}
return false;
}
static boolean stringIsValid(String s) {
int countOpenParths = 0;
int countCloseParths = 0;
for (var i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
countOpenParths++;
} else if (s.charAt(i) == ')') {
countCloseParths++;
}
}
return countCloseParths == countOpenParths;
}
}
Complexity - O(n)
1) Verify if String is valid
2) In loop get substring sequentially for first and last element
2.1) Verify if substring is valid and entire fits in parenthesis
2.2) If yes - continue loop for second element, etc..
2.3) if no - exit with current substring