Skip to content

Commit 9b6d070

Browse files
committed
Simple Symbols
1 parent 4371d6d commit 9b6d070

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/easy/SimpleSymbols.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package easy;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
/**
7+
* Have the function SimpleSymbols(str) take the str parameter being passed
8+
* and determine if it is an acceptable sequence by
9+
* either returning the string true or false.
10+
* The str parameter will be composed of + and = symbols
11+
* with several characters between them (i.e. ++d+===+c++==a)
12+
* and for the string to be true each letter must be surrounded
13+
* by a + symbol. So the string to the left would be false.
14+
* The string will not be empty and will have at least one letter.
15+
*/
16+
public class SimpleSymbols {
17+
18+
/**
19+
* Simple Symbols function.
20+
*
21+
* @param str input string
22+
* @return "true" if a sequence is acceptable
23+
*/
24+
private static String simpleSymbols(String str) {
25+
Pattern pattern1 = Pattern.compile("(?=(\\+\\w\\+))");
26+
Pattern pattern2 = Pattern.compile("[a-z]");
27+
Matcher matcher1 = pattern1.matcher(str);
28+
Matcher matcher2 = pattern2.matcher(str);
29+
int count1 = 0;
30+
int count2 = 0;
31+
int i1 = 0;
32+
int i2 = 0;
33+
while (matcher1.find(i1)) {
34+
count1++;
35+
i1 = matcher1.start() + 1;
36+
}
37+
while (matcher2.find(i2)) {
38+
count2++;
39+
i2 = matcher2.start() + 1;
40+
}
41+
return count1 == count2 ? "true" : "false";
42+
}
43+
44+
/**
45+
* Entry point.
46+
*
47+
* @param args command line arguments
48+
*/
49+
public static void main(String[] args) {
50+
var result1 = simpleSymbols("=+e++r+f+v+");
51+
System.out.println(result1);
52+
var result2 = simpleSymbols("=+e++r+ff+v+");
53+
System.out.println(result2);
54+
}
55+
56+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy