-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from amsharma44/string_reduction
String reduction
- Loading branch information
Showing
2 changed files
with
50 additions
and
57 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
class Solution { | ||
|
||
static int stringReduction (string a) { | ||
int[] ar = new int[3]; | ||
for(int i = 0; i < a.Length; i++) | ||
{ | ||
if(a[i] == 'a') | ||
{ | ||
ar[0]++; | ||
}else if(a[i] == 'b') | ||
{ | ||
ar[1]++; | ||
} | ||
else | ||
{ | ||
ar[2]++; | ||
} | ||
} | ||
|
||
if((ar[0] == 0 && ar[1] == 0) || (ar[1]== 0 && ar[2] == 0)||(ar[0] == 0 && ar[2] == 0)) | ||
{ | ||
return a.Length; | ||
} | ||
|
||
if ((ar[0] % 2 == 0 && ar[1] %2== 0) && (ar[1] %2 == 0 && ar[2] %2== 0)) | ||
{ | ||
return 2; | ||
} | ||
|
||
if ((ar[0] % 2 == 1 && ar[1] % 2 == 1) && (ar[1] % 2 == 1 && ar[2] % 2 == 1)) | ||
{ | ||
return 2; | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
static void Main(String[] args) { | ||
int res; | ||
int _t_cases = Convert.ToInt32(Console.ReadLine()); | ||
for (int _t_i=0; _t_i<_t_cases; _t_i++) { | ||
String _a = Console.ReadLine(); | ||
res=stringReduction(_a); | ||
Console.WriteLine(res); | ||
} | ||
} | ||
} |