“string seimbang di java” Kode Jawaban

string seimbang di java

	 public boolean isBalanced(String string) {
		HashMap<Character, Character> bracketMap = new HashMap<>();
		bracketMap.put('(', ')');
		bracketMap.put('[', ']');
		bracketMap.put('{', '}');
		
		Stack<Character> stack = new Stack<>();
		
		for(int count = 0; count < string.length(); count++) {
			char current = string.charAt(count);
			
			if(bracketMap.keySet().contains(current)) {
				stack.push(current);
			} else if(bracketMap.values().contains(current)) {
				if(!stack.empty() && bracketMap.get(stack.peek()) == current){
					stack.pop();
				} else {
					return false;
				}
			}
		}
		return stack.empty();
	}
Beautiful Batfish

string seimbang di java

    public static boolean checkBalanced(String check) {
        Stack<Character> S = new Stack<Character>();
        for(int a = 0; a < check.length(); a++){
            char let = check.charAt(a);
            if(let == '[' || let == '{' || let == '(')
                S.push(let);
            else if(let == ']' || let == '}' || let == ')')
            {
                if(S.empty())
                    return false;
                switch(let)
                {
                    // Opening square brace
                    case ']':
                        if (S.pop() != '[')
                            return false;
                        break;
                    // Opening curly brace
                    case '}':
                        if (S.pop() != '{')
                            return false;
                        break;
                    // Opening paren brace
                    case ')':
                        if (S.pop() != '(')
                            return false;
                        break;
                    default:
                        break;
                }
            }
        }
        if(S.empty())
            return true;
        return false;
    }
Beautiful Batfish

Jawaban yang mirip dengan “string seimbang di java”

Pertanyaan yang mirip dengan “string seimbang di java”

Lebih banyak jawaban terkait untuk “string seimbang di java” di Java

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya