SECTION
Published 2019. 11. 17. 23:52
[Codewar] Duplicate Encoder PS/Codewar

https://www.codewars.com/kata/duplicate-encoder/java

 

Codewars: Train your coding skills

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

www.codewars.com

Duplicate Encoder

The goal of this exercise is to convert a string to a new string where each character in the new string is "(" if that character appears only once in the original string, or ")" if that character appears more than once in the original string. Ignore capitalization when determining if a character is a duplicate.

 

Examples

"din"      =>  "((("
"recede"   =>  "()()()"
"Success"  =>  ")())())"
"(( @"     =>  "))((" 

 

Notes

Assertion messages may be unclear about what they display in some languages. If you read "...It Should encode XXX", the "XXX" is the expected result, not the input!

 

 

Solution

public class DuplicateEncoder {
	static String encode(String word){
  
	String[] list,list2 = new String[word.length()];
 
	list = word.toLowerCase().split("");
	String result = "";
		
		
		for(int i=0; i<list.length; i++)
		{
			for(int j=i+1; j<list.length; j++) 
			{
				if(list[i].equalsIgnoreCase(list[j])) 
				{
					list2[i] = ")";
					list2[j] = ")";
				}
			}
		}

		for(int k=0; k<list2.length; k++) 
		{
			if(list2[k]!=")") {
				list2[k]="(";
				result += list2[k];
			}
			else
				result += list2[k];
		}
		
	    return result;
  }
}
profile

SECTION

@SectionR0

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그