Question 1. Reverse String without spaces

  • Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
public class Solution {
    public String reverseWords(String s) {
        StringBuffer res = new StringBuffer();
        String[] words = s.split(" ");

        int idx = 0;

        for(int i = 0; i< s.length(); i++){
            if(s.charAt(i) == ' '){
                res.append(s.charAt(i));
            }

            if(s.charAt(i) != ' '){
                res.append(new StringBuffer(words[idx]).reverse().toString());
                i = i + words[idx].length() -1 ;
                idx++;
            }
        }

        return res.toString();

    }
}

Question 2. String to Integer

public class Solution {
    public String multiply(String num1, String num2) {
        char[] charArr1 = num1.toCharArray();
        char[] charArr2 = num2.toCharArray();

        int toNum1 = 0; double len1 = Math.pow(10,charArr1.length-1);
        int toNum2 = 0; double len2 = Math.pow(10,charArr2.length-1);

        for(char c : charArr1){
            toNum1 += ((c-'0') * len1);
            len1/=10;
        }
        for(char c : charArr2){
            toNum2 += ((c-'0') * len2);
            len2/=10;
        }        
        System.out.println(toNum1);
        System.out.println(toNum2);
        return String.valueOf(toNum1 * toNum2) ;

    }
}

Question 3. Remove dupliatecated character

public class Solution {
    static String super_reduced_string(String s){
        Stack<Character> stack = new Stack<Character>();
        StringBuffer sb = new StringBuffer("");
        for(char c : s.toCharArray()){
            if(!stack.isEmpty() && stack.peek() == c ){
                stack.pop();
            }else{
                stack.push(c);
            }
        }
        while(!stack.isEmpty()){
            sb.append(stack.pop());
        }   
        return sb.toString().equals("") ? "Empty String" : sb.reverse().toString();  
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        String result = super_reduced_string(s);
        System.out.println(result);
    }
}

Question 4. HackerRank in a String!

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int q = in.nextInt();
        for(int a0 = 0; a0 < q; a0++){
            String s = in.next();
            System.out.println(isGivenStr(s));
        }
    }

    public static String isGivenStr(String s){
        String str = "hackerrank";
        if(str.length() > s.length()){
            return "NO";
        }
        int i = 0;

        for(char c : s.toCharArray()){
            if(i == str.length()-1){
                break;
            }
            if(c == str.charAt(i)){
                i++;
            }
        }
        if(i == str.length()-1){
            return "YES";
        }else{
            return "NO";
        }
    }
}
  • 주어진 문자열에서 "hackerrank"만 찾으면 되므로, 주어진 문자열을 순차적으로 돌면서, hackerrank문자열의 인덱스와 비교한다.

Question 5. is pangram?

public class Solution {
    public static void main(String args[] ) throws Exception {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();

        if(s.length() < 26){
            System.out.println( "not pangram");
            return ;
        }
        boolean[] isExists = new boolean[26];
        int num = 26;
        for(char c : s.toCharArray()){
            if(num == 0){
                System.out.println("pangram");
                return ;
            }
            if(c >= 'a' && c <='z'){
                if(!isExists[c-'a']){
                    isExists[c-'a'] = true;
                    num--;
                }
            }else if(c >= 'A' && c <= 'Z'){
                if(!isExists[c-'A']){
                    isExists[c-'A'] = true;
                    num--;
                }
            }
        }
        if(num == 0){
            System.out.println("pangram");
        }else{
            System.out.println("not pangram");
        }
    }
}

Question 6. reverse String to Int?

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] arr = s.split(" ");
        StringBuffer i = new StringBuffer(arr[0]);
        StringBuffer j = new StringBuffer(arr[1]);
        StringBuffer k = new StringBuffer(arr[2]);
        int ret = 0;

        for(int start = Integer.parseInt(i.toString()); start <= Integer.parseInt(j.toString());start++ ){
            int reverse = Integer.parseInt(new StringBuffer(String.valueOf(start)).reverse().toString());
            if(Math.abs((float)start - (float)reverse) % Integer.parseInt(k.toString()) == 0 ){
                ret++;
            }
        }
        System.out.println(ret); 
    }
}

Question 7. Find weight uniformed substring?

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        HashSet<Integer> set = new HashSet<Integer>();
        String s = in.next();
        int lastAlphabet = (int)s.charAt(0) ;
        int contigious = 1;
        set.add((lastAlphabet-'a' +1) * contigious);


        for(int i =1; i<s.length(); i++){
            if(s.charAt(i) == lastAlphabet){
                contigious++;
            }
            if(s.charAt(i) != lastAlphabet){
                lastAlphabet = (int)s.charAt(i);
                contigious =1;
            }

            if(!set.contains((lastAlphabet - 'a' +1) * contigious)){
                set.add((lastAlphabet - 'a' + 1) * contigious);
            }
        }
        int n = in.nextInt();
        for(int a0 = 0; a0 < n; a0++){
            int x = in.nextInt();
            if(set.contains(x)){
                System.out.println("Yes");
            }else{
                System.out.println("No");
            }
        }
    }
}

Question 8. Separate the Numbers

  • Decide given string contains beautiful number or not
  • 1234- -> 1 2 3 4
  • 123124125 -> 123 124 125
  • 99100101 -> 99 100 101
  • 991011 -> X
public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int q = in.nextInt();
        for(int a0 = 0; a0 < q; a0++){
            String s = in.next();
            System.out.println(helper(s));
        }
    }
    public static String helper(String s){
        String ret = "NO";
        if(s.charAt(0) == '0'){
            return ret;
        }
        long temp = 0;
        String min ="";
        String testStr = "";
        for(int i = 1; i<=s.length()/2 ; i++){
            testStr = s.substring(0,i);
            temp = Long.parseLong(testStr);
            min = testStr;                      
            while(testStr.length() < s.length()){
                temp++;
                testStr += String.valueOf(temp);
            }
            if(String.valueOf(testStr).equals(s)){
                ret = "YES "+ min;
                break;
            }
        }      
        return ret;     
    }
}
  • 최대 서브 문자열의 길이는 전체 문자열 /2 이다. 그리고 윈도우 사이즈를 정하자

Question 9. Funny String

  • String R is reverse of given S
  • |i - (i-1)| of S is same as |i-(i-1)| of R is funny String
  • Decide given S is a funny string or not
public class Solution {

    static String funnyString(String s){
        for(int i =1; i<s.length(); i++){
            if(Math.abs(s.charAt(i)-s.charAt(i-1)) != Math.abs( s.charAt(s.length()-i) - s.charAt(s.length()-i-1))){
                return "Not Funny";
            }
        }
        return "Funny";
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int q = in.nextInt();
        for(int a0 = 0; a0 < q; a0++){
            String s = in.next();
            String result = funnyString(s);
            System.out.println(result);
        }
    }
}

results matching ""

    No results matching ""