You are on page 1of 8

Answer.

import java.util.Scanner;

public class TreasureBox {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String note = sc.nextLine();
        String[] words = note.split(" ");
        int code = 0;
        for (String word : words) {
            int digit = 0;
            for (int i = 0; i < word.length(); i++) {
                int wd = (int) word.charAt(i);
                if (wd >= 65 && wd <= 90) {
                    digit += wd - 64;
                } else if (wd >= 97 && wd <= 122) {
                    digit -= wd - 96;
                }
            }
            digit = Math.max(0, digit);
            digit = Math.min(9, digit);
            code = code * 10 + digit;
        }
        System.out.println(" " + code);
    }
}

Answer.2

import java.util.*;
class Decipher2
{
    Scanner sc=new Scanner(System.in);
    String str;
   String[] code;
   int count=1;
    char[] defaultChar= {'c','d','a','e','l','i','m','o','u','v'};
    Decipher2()
  {
        str=sc.nextLine();
        for(int i=0;i<str.length();i++)
      {
            if(str.charAt(i)==' ')
                count++;
        }
        code=new String[count];
     Arrays.fill(code, "");
    }

    void decipher()
  {
        int i=0,j=0,k=0, wd=0;
        while(i<str.length())
     {
       
            if(str.charAt(i)==' ')
        {
           i++;
           code[j]+=" ";
           j++;
        }
        wd =(int)str.charAt(i);
            k=wd-48;
            code[j]+=defaultChar[k];
        i++;
        }
        for(j=0;j<code.length;j++)
     {
            System.out.print(code[j]);
        }
    }
}
public class dec2
{
    public static void main(String[] args)
   {
        Decipher2 obj= new Decipher2();
        obj.decipher();
    }
}
Answer.3

import java.util.Scanner;

public class AlienTranslator {


    public static String translate(String input) {
        String[] words = input.split(" ");
        StringBuilder sb = new StringBuilder();
        for (int i = words.length - 1; i >= 0; i--) {
            sb.append(words[i]);
            sb.append(" ");
        }
        return sb.toString().trim();
    }

    public static void main(String[] args) {


        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String input = scanner.nextLine();
        System.out.println("Translation: " + translate(input));
    }
}
Answer.4

import java.util.*;

public class Main_1 {


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(" ");
        String namesInput = scanner.nextLine();
        String[] names = namesInput.split(" ");
        Map<String,Integer> namesMap = new HashMap<>();
        for (String name : names) {
            String[] parts = name.split("(?=[A-Z])");
            String firstName = parts[0];
            if(!namesMap.containsKey(firstName)){
                namesMap.put(firstName,1);
            }else {
                namesMap.put(firstName,namesMap.get(firstName)+1);
            }
        }
        int maxCount = Collections.max(namesMap.values());
        List<String> mostCommonNames = new ArrayList<>();
        for(Map.Entry<String,Integer> entry : namesMap.entrySet()){
            if(entry.getValue() == maxCount)
mostCommonNames.add(entry.getKey());
        }
        Collections.sort(mostCommonNames);
        System.out.println(" " + mostCommonNames.get(0));
    }
}
Answer.5

import java.util.Scanner;

public class Main {


    public static int minDeletions(String s) {
        int n = s.length();
        int[][] dp = new int[n][n];
        for (int i = 0; i < n; i++) {
            dp[i][i] = 0;
        }
        for (int len = 2; len <= n; len++) {
            for (int i = 0; i < n - len + 1; i++) {
                int j = i + len - 1;
                if (s.charAt(i) == s.charAt(j)) {
                    dp[i][j] = dp[i + 1][j - 1];
                } else {
                    dp[i][j] = 1 + Math.min(dp[i][j - 1], dp[i + 1][j]);
                }
            }
        }
        return dp[0][n - 1];
    }

    public static void main(String[] args) {


        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        System.out.println(minDeletions(s));
    }
}
Answer.6

import java.util.Scanner;

public class PangramCheck {


    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        scan.nextLine(); // to consume the newline character
        for (int i = 0; i < n; i++) {
            String sentence = scan.nextLine().toLowerCase();
            boolean[] alphabet = new boolean[26];
            int index = 0;
            for (int j = 0; j < sentence.length(); j++) {
                if (sentence.charAt(j) >= 'a' && sentence.charAt(j) <= 'z') {
                    index = sentence.charAt(j) - 'a';
                    alphabet[index] = true;
                }
            }
            boolean isPangram = true;
            for (int j = 0; j <= 25; j++) {
                if (alphabet[j] == false) {
                    isPangram = false;
                    break;
                }
            }
            if (isPangram) {
                System.out.println("Pangram");
            } else {
                System.out.println("Not Pangram");
            }
        }
        scan.close();
    }
}
Answer.7

import java.util.HashMap;
import java.util.Scanner;

public class RomanToInteger {


    public static int romanToInt(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        map.put('I', 1);
        map.put('V', 5);
        map.put('X', 10);
        map.put('L', 50);
        map.put('C', 100);
        map.put('D', 500);
        map.put('M', 1000);

        int result = 0;
        for (int i = 0; i < s.length(); i++) {
            int current = map.get(s.charAt(i));
            int next = (i < s.length() - 1) ? map.get(s.charAt(i + 1)) : 0;
            if (current < next) {
                result += next - current;
                i++;
            } else {
                result += current;
            }
        }
        return result;
    }

    public static void main(String[] args) {


        Scanner scanner = new Scanner(System.in);
        System.out.print(" ");
        String roman = scanner.nextLine();
        int integer = romanToInt(roman);
        System.out.println(" " + integer);
    }
}
Answer.8

import java.util.Scanner;

public class IntegerToRoman {


    public static String intToRoman(int num) {
        String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X",
"IX", "V", "IV", "I"};
        int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};

        StringBuilder roman = new StringBuilder();


        for (int i = 0; i < values.length; i++) {
            while (num >= values[i]) {
                num -= values[i];
                roman.append(symbols[i]);
            }
        }
        return roman.toString();
    }

    public static void main(String[] args) {


        Scanner scanner = new Scanner(System.in);
        System.out.print(" ");
        int number = scanner.nextInt();
        String roman = intToRoman(number);
        System.out.println(" " + roman);
    }
}

You might also like