Python main.py · 12 行 def is_balanced(s): match = {")": "(", "]": "[", "}": "{"} stack = [] for c in s: if c in "([{": stack.append(c) elif not stack or stack.pop() != match[c]: return False return not stack print("はい" if is_balanced(input().strip()) else "いいえ") JavaScript main.js · 13 行 const s = require("fs").readFileSync(0, "utf8").trim(); function isBalanced(s) { const match = { ")": "(", "]": "[", "}": "{" }; const stack = []; for (const c of s) { if ("([{".includes(c)) stack.push(c); else if (stack.length === 0 || stack.pop() !== match[c]) return false; } return stack.length === 0; } console.log(isBalanced(s) ? "はい" : "いいえ"); TypeScript main.ts · 14 行 import { readFileSync } from "node:fs"; const s: string = readFileSync(0, "utf8").trim(); function isBalanced(s: string): boolean { const match: Record<string, string> = { ")": "(", "]": "[", "}": "{" }; const stack: string[] = []; for (const c of s) { if ("([{".includes(c)) stack.push(c); else if (stack.length === 0 || stack.pop() !== match[c]) return false; } return stack.length === 0; } console.log(isBalanced(s) ? "はい" : "いいえ"); Java Main.java · 25 行 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.Deque; public class Main { static boolean isBalanced(String s) { Deque<Character> stack = new ArrayDeque<>(); for (char c : s.toCharArray()) { if (c == '(' || c == '[' || c == '{') { stack.push(c); } else { char open = c == ')' ? '(' : c == ']' ? '[' : '{'; if (stack.isEmpty() || stack.pop() != open) return false; } } return stack.isEmpty(); } public static void main(String[] args) throws IOException { String s = new BufferedReader(new InputStreamReader(System.in)).readLine().trim(); System.out.println(isBalanced(s) ? "はい" : "いいえ"); } } C# Main.cs · 29 行 using System; using System.Collections.Generic; class Program { static bool IsBalanced(string s) { var stack = new Stack<char>(); foreach (char c in s) { if (c == '(' || c == '[' || c == '{') { stack.Push(c); } else { char open = c == ')' ? '(' : c == ']' ? '[' : '{'; if (stack.Count == 0 || stack.Pop() != open) return false; } } return stack.Count == 0; } static void Main() { string s = Console.ReadLine().Trim(); Console.WriteLine(IsBalanced(s) ? "はい" : "いいえ"); } } C++ main.cpp · 23 行 #include <iostream> #include <stack> #include <string> bool isBalanced(const std::string& s) { std::stack<char> st; for (char c : s) { if (c == '(' || c == '[' || c == '{') { st.push(c); } else { char open = c == ')' ? '(' : c == ']' ? '[' : '{'; if (st.empty() || st.top() != open) return false; st.pop(); } } return st.empty(); } int main() { std::string s; std::cin >> s; std::cout << (isBalanced(s) ? "はい" : "いいえ") << '\n'; } C main.c · 24 行 #include <stdbool.h> #include <stdio.h> static char s[10001], stack[10000]; bool is_balanced(const char *s) { int top = 0; for (int i = 0; s[i]; i++) { char c = s[i]; if (c == '(' || c == '[' || c == '{') { stack[top++] = c; } else { char open = c == ')' ? '(' : c == ']' ? '[' : '{'; if (top == 0 || stack[--top] != open) return false; } } return top == 0; } int main(void) { scanf("%10000s", s); puts(is_balanced(s) ? "はい" : "いいえ"); return 0; } Craykon main.cray · 28 行 짝맞나(s) { stack = []; それぞれ (c の中 s) { もし (c == "(" || c == "[" || c == "{") { stack.追加(c); } でなければ { もし (stack.個数 == 0) { 返す 偽; } top = stack[stack.個数 - 1]; stack.位置で取り除く(stack.個数 - 1); もし (c == ")" && top != "(" || c == "]" && top != "[" || c == "}" && top != "{") { 返す 偽; } } } 返す stack.個数 == 0; } s = コンソール.行読み().空白除去(); コンソール.書く(짝맞나(s) ? "はい" : "いいえ");