Anda di halaman 1dari 4

Problem Statement You just bought a very delicious chocolate bar from a local store.

This chocolate bar consists of N squares, numbered 0 through N-1. All the squares are arranged in a single row. There is a letter carved on each square. You are given a String letters. The i-th character of letters denotes the letter carved on the i-th square (both indices are 0-based). You want to share this delicious chocolate bar with your best friend. At first, you want to give him the whole bar, but then you remembered that your friend only likes a chocolate bar without repeated letters. Therefore, you want to remove zero or more squares from the beginning of the bar, and then zero or more squares from the end of the bar, in such way that the remaining bar will contain no repeated letters. Return the maximum possible length of the remaining chocolate bar that contains no repeated letters. Definition
Class: Method: Parameters: Returns: ChocolateBar maxLength String int

Method signature: int maxLength(String letters) (be sure your method is public)

Constraints - letters will contain between 1 and 50 characters, inclusive. - Each character of letters will be a lowercase letter ('a' - 'z'). Examples 0)
"srm" Returns: 3

You can give the whole chocolate bar as it contains no repeated letters.

1)
"dengklek" Returns: 6

Remove two squares from the end of the bar.

2)
"haha" Returns: 2

There are three possible ways:


Remove two squares from the beginning of the bar. Remove two squares from the end of the bar. Remove one square from the beginning of the bar and one square from the end of the bar.

3)
"www" Returns: 1

4)
"thisisansrmbeforetopcoderopenfinals" Returns: 9

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
1. #include<iostream> 2. #include<string.h> 3. #include<string> 4. using namespace std; 5. 6. class ChocolateBar{ 7. 8. private:

9. 10. 11.

int len; int counter[30]; bool flag[30];

12. bool check; 13. public: 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. }; } } return maximum; }//end method maxLength } if(check==true&&(j-i+1)>maximum) maximum=j-i+1; int maxLength(string letters){ len=letters.length(); int maximum=0; for(int i=0;i<len;i++){ for(int j=i;j<len;j++){ memset(flag,false,sizeof(flag)); check=true; for(int k=i;k<=j;k++){ if(flag[letters[k]-'a']==true){ check=false; break; } else flag[letters[k]-'a']=true;

---------------------

Chocolate Bar - Solution : using System; using System.Collections; using System.Collections.Generic; public class ChocolateBar { public int maxLength(string letters) { int max = 0; for (int i = 0; i <= letters.Length - 1; i++) { for (int j = i; j <= letters.Length - 1; j++) { int c = GetCount(letters, i, j);

if (c > max) max = c; } } return max; } int GetCount(string letters, int strIdx, int lstIdx) { Dictionary<char, int> letterCount = new Dictionary<char, int>(); for (int i = strIdx; i <= lstIdx; i++) { if (letterCount.ContainsKey(letters[i])) { return 0; } letterCount[letters[i]] = 1; } return lstIdx - strIdx + 1; } }

Anda mungkin juga menyukai