Skip to content

Commit 5512a17

Browse files
authored
Create StringReduction.java
1 parent 19f2f4c commit 5512a17

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Hard/StringReduction.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
class Main {
5+
6+
public static int StringReduction(String str) {
7+
// code goes here
8+
int a=0,b=0,c=0;
9+
10+
for (int i =0 ; i<str.length(); i++){
11+
if (str.charAt(i)=='a') a++;
12+
13+
if (str.charAt(i)=='b') b++;
14+
15+
if (str.charAt(i)=='c') c++;
16+
17+
}
18+
19+
if (a==str.length() || b==str.length() || c==str.length()) return str.length();
20+
21+
if ((a%2==0 && b%2==0 && c%2==0) || (a%2==1 && b%2==1 && c%2==1)){
22+
return 2;
23+
24+
}else{
25+
26+
return 1;
27+
28+
}
29+
}
30+
31+
public static void main (String[] args) {
32+
// keep this function call here
33+
Scanner s = new Scanner(System.in);
34+
System.out.print(StringReduction(s.nextLine()));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)