Questions tagged [balanced-string]

In a balanced string certain pairs of characters or substrings always appear with the same amount. Often there are additional restrictions that those pairs also have to be ordered. The most common example is a correctly parenthesized string. See also the [palindrome] and [syntax] tags.

Filter by
Sorted by
Tagged with
13 votes
15 answers
2k views

Can you perform swaps?

Today's problem is easy. You're given two strings A and B of equal length consisting of only the characters ( and ). Check ...
Huỳnh Trần Khanh's user avatar
19 votes
24 answers
2k views

Split a string on unnested commas

Create the shortest program/function/whatever that splits an inputted string along un-nested commas. A comma is considered nested if it is either within parentheses, brackets, or braces. Input and ...
Dadsdy's user avatar
  • 2,035
10 votes
9 answers
969 views

Language Word Checker

Develop a program to check if a given word is part of a language defined by the following rules: The language has a set of rules. The empty string is considered a valid word in the language. Two ...
Huỳnh Trần Khanh's user avatar
13 votes
18 answers
3k views

Longest Valid Parentheses

Given a string of parentheses ( and ), find the length of the longest substring that forms a valid pair of parentheses. Valid ...
Aitzaz Imtiaz's user avatar
15 votes
5 answers
432 views

Flip the order of operations

Scientists have made contact with a parallel universe. Just one problem: They write math differently. Help the scientists make a program that can convert normal math expressions to their equivalent in ...
mousetail's user avatar
  • 11.8k
9 votes
8 answers
525 views

CGCC Rocket Biking

Something I found while looking through some old files. It seemed like a neat idea for a code golf challenge. The intro One of the most popular events at the annual Cyclist's Grand Competition for ...
mindoverflow's user avatar
  • 1,043
23 votes
16 answers
4k views

Remove redundant parentheses

Given a string containing some parentheses and some other ASCII printable characters, like this: (abc((123))(k)) your task is to remove any sets of parentheses that ...
mousetail's user avatar
  • 11.8k
20 votes
28 answers
3k views

Rearrange to a palindrome

Given a string, shuffle it so that it becomes a palindrome. For example, adadbcc can be arranged into dacbcad, or ...
emanresu A's user avatar
  • 35.6k
18 votes
25 answers
2k views

Is it a valid list?

Given a string like [[[],[[]]],[]], made of only commas and square brackets, your challenge is to determine whether it represents a list. A list is either: ...
emanresu A's user avatar
  • 35.6k
10 votes
6 answers
425 views

Parenthetical Names

Parenthetical names Your challenge is to build a program that puts parentheses around the 'title' part of names. For example, Sonic the Hedgehog is converted to <...
Hedge Fleming's user avatar
16 votes
23 answers
2k views

Pair my brackets

Given a string consisting of ()[]{}, print the paired brackets in the same order as they appear in the string. Any opening bracket ...
math scat's user avatar
  • 8,543
13 votes
9 answers
1k views

Remove unmatched brackets

Inspired by @AviFS. Given a string containing brackets, e.g. [xyz]]abc[[def]hij[ You can parse through it with a stack of brackets. When you find an open bracket, ...
emanresu A's user avatar
  • 35.6k
10 votes
6 answers
488 views

The Missing Match

You are given a string of unknown length that contains a varied amount of (, {, [ and their ...
code's user avatar
  • 531
3 votes
1 answer
325 views

Bracket Depth List

Challenge Your challenge is simple, calculate the depth of each matching brackets in the given input e.g. (()()(()))->...
DialFrost's user avatar
  • 5,053
28 votes
18 answers
2k views

Find the index of the matching parentheses for each character

The challenge is simple, find the matching parentheses for every one of the parentheses in a given string input E.g. ()()() -> [1, 0, 3, 2, 5, 4] Always start ...
DialFrost's user avatar
  • 5,053
25 votes
5 answers
1k views

Ungolf my tinylisp code

I like golfing in tinylisp: (d M(q((x)(i x(i(disp x)0(M x))0 But I also like posting explanations with nicely formatted code: ...
DLosc's user avatar
  • 38k
16 votes
18 answers
961 views

AoCG2021 Day 11: Garbageful streams

Part of Advent of Code Golf 2021 event. See the linked meta post for details. Related to AoC2017 Day 9. Weekends are Bubbler's days off from posting these lol A large stream blocks your path. ...
lyxal's user avatar
  • 32.2k
29 votes
31 answers
3k views

Open or Close the Parentheses

Every phone needs a calculator app. Because of the limited screen real estate, the developer of mine has decided to save some buttons. Instead of having two dedicated bracket buttons - one for open <...
Jitse's user avatar
  • 6,876
11 votes
0 answers
653 views

Lean golf: Balanced Bracket Sequence

Ungolfed, ugly, horrible proof to help you make progress on this challenge: https://gist.github.com/huynhtrankhanh/dff7036a45073735305caedc891dedf2 A bracket sequence is a string that consists of the ...
Huỳnh Trần Khanh's user avatar
17 votes
22 answers
2k views

Parentheses Clusters

Write a program that groups a string into parentheses cluster. Each cluster should be balanced. Examples : ...
MSS2001's user avatar
  • 339
21 votes
31 answers
2k views

Remove oddly nested substrings

Task: Input will consist of some text, with nested [] separating some of it into nested groups: ...
Rydwolf Programs's user avatar
17 votes
12 answers
2k views

Remove All Smalltalk Comments for Code Golf

Take a line of input (function arguments or stdin, etc.). Remove comments in it, where a comment starts and ends with ". Do not remove comments in string ...
Wezl's user avatar
  • 1,458
20 votes
11 answers
4k views

I'm Lazy: Close my Superparens

Picolisp has a feature called "super parentheses": Brackets ('[' and ']') can be used as super parentheses. A closing bracket will match [all parentheses to and including] the innermost ...
Wezl's user avatar
  • 1,458
16 votes
14 answers
1k views

A bracket amphitheater

Specification Write the shortest possible code in any language that does the following: It takes two arguments, x and y, and generates x + y lines of text. The first x lines should consist of y nested ...
schuelermine's user avatar
28 votes
34 answers
3k views

Rotate brackets until they're balanced

Take the string of brackets ]][][[. When you rotate it to the right once, you get []][][. If you rotate it again, you get ...
Rydwolf Programs's user avatar
2 votes
0 answers
85 views

Find the number of parenthesis combinations [duplicate]

For example, given 3 sets of parenthesis, you have: ()()() ((())) ()(()) (())() (()()) = 5 possible combinations. Challenge Program must: • Take 1 number as an ...
Camden Weaver's user avatar
4 votes
4 answers
3k views

Parse a nested parentheses

Objective Mimic Haskell's reads :: ReadS (). Or in other words, parse nested parentheses. Since the target type is () (the type ...
Dannyu NDos's user avatar
  • 5,497
1 vote
0 answers
51 views

Bracket Checker [duplicate]

Given a string of brackets tell the user whether or not the brackets are well matched or not. Brackets include []{}(). For example: ...
Ramen_Lover912's user avatar
18 votes
14 answers
3k views

Visualize a Recursive Acronym

Background Famously, the acronym GNU stands for GNU's Not Unix. 1 It's recursive because, after expanding it once, it still ...
Jonah's user avatar
  • 33k
14 votes
4 answers
613 views

Keta Bracket Autocompletion

Special thanks to Bubbler and AdmBorkBork for supplying feedback about this challenge Keta (Keyboard tacit — clever naming, I know!) is a golfing language I’ve been working on recently. One of it’s ...
lyxal's user avatar
  • 32.2k
14 votes
17 answers
3k views

Parentheses sequences in lexicographical order

Challenge Taken from here and also here An n parentheses sequence consists of n (s and n )s. A valid parentheses sequence is ...
Luis felipe De jesus Munoz's user avatar
15 votes
22 answers
3k views

Test if a string is parenthesly balanced

We call a parens group the open paren (, its matching close paren ) and everything inside them. A parens group or string is ...
ibrahim mahrir's user avatar
6 votes
2 answers
217 views

What associativity was it again

Non-associative operators (for example the subtraction-operator) often are either left- or right associative, such that one has to write less parentheses. Consider for example the following: $$ a-b-c ...
ბიმო's user avatar
  • 16.7k
26 votes
25 answers
2k views

Detect Perfect Pairings

Let's have a function \$f\$ that takes a string and removes all pairs of adjacent identical characters. For example \$f(a\color{red}{bb}ba\color{red}{cc}) = aba\$ Note that when two pairs overlap ...
Wheat Wizard's user avatar
  • 96.5k
25 votes
25 answers
3k views

Golf A Parentheses Matching Algorithm

You will be given a string s. It is guaranteed that the string has equal and at least one [s and ...
Windmill Cookies's user avatar
27 votes
12 answers
2k views

Expand compressed brain-flak

This challenge was posted as part of the April 2018 LotM challenge, as well as for Brain-flak's 2nd birthday I was thinking about what the most efficient way to encode brain-flak programs would be. ...
DJMcMayhem's user avatar
  • 58.9k
15 votes
11 answers
1k views

Balanced Zero-One Encoding

Task Encode a string that entirely consists of uppercase alphabets (A-Z) using only zeros and ones, using your own favorite scheme. But the rule isn't that simple! ...
Bubbler's user avatar
  • 73.8k
14 votes
3 answers
510 views

Flatten a Stack Cats program

Stack Cats is a reversible, stack-based language. Its reversible nature makes for somewhat weird loops. This challenge is about the conditional loop (...). When ...
Martin Ender's user avatar
18 votes
9 answers
648 views

Fill in the Bracks

Normal brackets ((),[],<> and {}) are nice and ...
Wheat Wizard's user avatar
  • 96.5k
8 votes
2 answers
398 views

Keep Decoding This Number!

This challenge posed an algorithm for encoding an integer n as another integer r. What follows is a succinct explanation of that ...
Sherlock9's user avatar
  • 12.2k
13 votes
6 answers
356 views

Decode Factor Trees

In case you missed Encode Factor Trees, here is the definition of a Factor Tree: The empty string is 1. Concatenation represents multiplication. A number n enclosed in parentheses (or any ...
Nissa's user avatar
  • 3,616
24 votes
3 answers
1k views

Clearly parenthesize APL trains

In APL, you can write tacit functions, called trains. How they work is irrelevant for this challenge. Here are the different ways they can be grouped, using as ...
Erik the Outgolfer's user avatar
13 votes
7 answers
406 views

Encode Factor Trees [duplicate]

I once saw on the xkcd fora a format for expressing numbers in an odd way. In this "factor tree" format: The empty string is 1. Concatenation represents multiplication. A number n enclosed in ...
Nissa's user avatar
  • 3,616
27 votes
5 answers
657 views

Bubble the brackets!

There are a few questions on this site about balancing brackets, and checking whether brackets are balanced. I propose it's now time to use those balanced brackets for something! In mathematics and ...
Arthur's user avatar
  • 371
21 votes
14 answers
2k views

Turn a string inside out

A balanced string is a string of parentheses () so that every parenthesis is can be matched with another one. More rigorously they are the strings spanned by this ...
Wheat Wizard's user avatar
  • 96.5k
43 votes
25 answers
4k views

(A[l(t[e(r)n]e)s]t) a string!

Alternesting, is the act of taking a string and nesting it in alternating brackets. Here is how you alternest a string. For a string of length N, take the center N characters, and surround them in ...
DJMcMayhem's user avatar
  • 58.9k
13 votes
3 answers
343 views

Quote my quote!

A quotation is "the repetition of one expression as part of another one". In American English, a quotation is surrounded by double-quotation marks ("), as in the previous sentence. However, this rule ...
musicman523's user avatar
  • 4,772
3 votes
17 answers
3k views

Is there an unmatched parenthesis in this String?

Introduction Given a String containing an arithmetic expression, your task is to output a truthy or falsey value based on whether it contains unmatched parentheses. Input Your program should take ...
Arjun's user avatar
  • 5,034
25 votes
6 answers
536 views

Appends or Prepends? Depends

Brain-flak turns one year old tomorrow! In honor of it's birthday, we're having a PPCG style birthday party, where several users post brain-flak related questions! Help us celebrate! :) Brain-flak is ...
DJMcMayhem's user avatar
  • 58.9k
22 votes
22 answers
1k views

Find the First Bracket Match

This was one of a series of challenges leading up to Brain-Flak's birthday. Find out more here. Challenge For this challenge your objective will be to find the very first pair of matching brackets in ...
Pavel's user avatar
  • 9,369