object Scala extends App { class Utils { def upper(entries: String*) = entries.map(_.toUpperCase) } System.out.print(new Utils().upper("A", "First", "Scala", "Program") mkString " ") }
- object Scala extends App {
class StringUtilities {def upper(strings: String*): Seq[String] = {strings.map((s:String) => s.toUpperCase())}- class Utils {
- def upper(entries: String*) = entries.map(_.toUpperCase)
- }
val up = new StringUtilitiesConsole.println(up.upper("A", "First", "Scala", "Program"))- System.out.print(new Utils().upper("A", "First", "Scala", "Program") mkString " ")
- }
Stacks
Arrays
Data Types
import java.util.*; public class MatchingBrackets { public static boolean isBalanced(String braces) { Stack<String> stack = new Stack<>(); Map<String, String> enclosing = new HashMap<>(); enclosing.put(")","("); enclosing.put("}","{"); enclosing.put("]","["); for (char brace : braces.toCharArray()) { final String strBrace = Character.toString(brace); if (!stack.isEmpty() && stack.peek().equals(enclosing.get(strBrace))) { stack.pop(); } else { stack.push(strBrace); } } return stack.isEmpty(); } }
/*** Check if brackets are matching.* Time Complexity: O(N), Space Complexity: O(N)** @author Jayesh Chandrapal*/- import java.util.*;
import java.lang.*;import java.io.*;class MatchingBrackets {/*** Checks if given input string contains matching brackets.** @params str input string to be checked* @returns boolean value indicating if given string contains matching brackets*/public static boolean isBalanced(String str) {boolean balanced = true;Stack<Character> stack = new Stack<Character>();for(Character c : str.toCharArray()) {if(c == '(' || c == '{' || c == '[') {stack.push(c);- public class MatchingBrackets {
- public static boolean isBalanced(String braces) {
- Stack<String> stack = new Stack<>();
- Map<String, String> enclosing = new HashMap<>();
- enclosing.put(")","(");
- enclosing.put("}","{");
- enclosing.put("]","[");
- for (char brace : braces.toCharArray()) {
- final String strBrace = Character.toString(brace);
- if (!stack.isEmpty() && stack.peek().equals(enclosing.get(strBrace))) {
- stack.pop();
- } else {
if(stack.isEmpty()) {balanced = false;break;}Character top = stack.peek();if((c == ')' && top == '(') || (c == '}' && top == '{') || (c == ']' && top == '[')) {stack.pop();} else {balanced = false;break;}- stack.push(strBrace);
- }
}return balanced && stack.isEmpty();}- }
- return stack.isEmpty();
- }
- }