글자 단어 개수 세기 [Java]

문제 설명

 

내 코드

import java.io.*;
import java.util.*;



class Main{

	public static void main(String [] args){
    
    	Scanner scanner = new Scanner(System.in);
        
        while(scanner.hasNextLine()){    
        	String str = scanner.nextLine();
        	int words=0, letters=0;


         	String []word = str.split(" \t");
         	words = word.length;
          
          
         	for(int i=0; i<str.length; i++){
          		if( str.charAt(i) != " " && str.charAt(i) != "\t")
                	letters+=1;
          	}

          System.out.println(words + " " + letters);
        }
        
    }
}

 

🔑 Key Point 🔑

String객체에 쓰는 split 함수 

String [] s = str.split(" ");  str을 공백의 단위로 쪼개어 s 배열에 단어를 자동 넣어준다 !

UVa Online Judge - 10252번 Common Permutation

[Java]  공통된 변경 문자열(Common Permutation)

 

문제 설명

 

 

 

내 코드

import java.io.*;
import java.util.*;

class Main {
	
	public static void main(String[] args) throws Exception {
		
		Scanner scanner = new Scanner(System.in);
		
		while(scanner.hasNextLine()){
	
			String input1 =scanner.nextLine();
			String input2 =scanner.nextLine();
			
			Vector<Character> str1 = new Vector<Character>();
			Vector<Character> str2 = new Vector<Character>();
			ArrayList<Character> result= new ArrayList<Character>();
			
			//입력 스트링 벡터에 저장
			for(int j=0; j<input1.length();j++)
				str1.add(input1.charAt(j));
			
			for(int j=0; j<input2.length();j++)
				str2.add(input2.charAt(j));
			

			//하나하나 대조하여 발견되면 벡터에서 제거해준다.
			for(int j=0; j<str1.size(); j++){
				for(int k=0; k<str2.size();k++){
					if(str1.get(j)==str2.get(k)){
						result.add(str1.get(j));
						str2.remove(k);
						break;
					}
				}
			}
			
			//오름차순 정렬
			Collections.sort(result, new Comparator<Character>(){
				public int compare(Character o1,Character o2){
					return o1.compareTo(o2);
				}
			});

			
			//출력하기
			for(char c:result)
				System.out.print(c);
			System.out.println("");
			
		}
			
		
		scanner.close();
			
	}	
}

 

 

🔑Key Point🔑

그냥 다른 포함된 것을 찾으면 안되고, 포함되었다면 그부분은 삭제해줘야 중복이 생기지 않는다.

+ Recent posts