Pregunta de entrevista de Capital One

Implement a function for determining the validity of a string. The string is valid if parentheses are correctly distributed within the string.

Respuestas de entrevistas

Anónimo

27 feb 2016

Most of your answers fail. You only count to make sure the number of left parentheses equals the number of right parentheses. You don't check if they are valid. Example ')()(' would pass your checks when it should fail.

6

Anónimo

13 feb 2017

public static boolean check(String s){ int openParen = 0; for(int i = 0; i < s.length(); i++){ if(s.charAt(i) == '('){ openParen++; } if(s.charAt(i) == ')'){ if(openParen == 0){ return false; } openParen--; } } return openParen == 0; }

1

Anónimo

1 may 2016

#include using namespace std; int main() { int l=0; int r=0; string str = ""; for(char& c : str) { if(c=='(') l++; else if(c==')' && r!=l) r++; else { cout << "invalid string" << endl; return 0; } } if(l==r)cout << "valid string" << endl; }

1

Anónimo

9 jul 2016

public boolean isBalanced(String text) { int left = 0; int right = 0; for (int i = 0; i left) { return false; } } return (left == right); }

Anónimo

9 jul 2016

public boolean isBalanced(String text) { int left = 0; int right = 0; for (int i = 0; i left) { return false; } } return (left == right); }

Anónimo

15 nov 2015

def iterator(string): length = len(string) listOfIndices = [] for i in range(length): if string[i] == '(': stillValid = findMatch(i, string, listOfIndices) if not stillValid: return False for i in range(length): if string[i] == ')': if i not in listOfIndices: return False return True def findMatch(currIndex, string, listOfIndices): length = len(string) if currIndex == length -1: return False for i in range(currIndex+1,length): if i not in listOfIndices: if string[i]==')': listOfIndices.append(i) return True return False def main(): string = "(())(" print iterator(string) string = "(" print iterator(string) string = "hey)" print iterator(string) string = ")()()()(" print iterator(string) string = "(()()(mnmn)00)" #should be valid print iterator(string)

2

Anónimo

19 nov 2015

#return true for valid, false for invalid def validParens(string): length = len(string) left = 0 right = 0 for i in range(length): if string[i] == "(": left += 1 elif string[i] == ")": right += 1 if right > left: return False return True

1

Anónimo

9 nov 2015

Public Boolean check(string str) { Int left_count = 0; Int right_count = 0; For(int x =0; x

3