- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsDoubloon.java
More file actions
Latest commit
82 lines (68 loc) · 2.49 KB
/
Copy pathIsDoubloon.java
File metadata and controls
82 lines (68 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Chapter 7.9
*/
importjava.util.Arrays;
importjava.util.Scanner;
publicclassIsDoubloon
{
publicstaticvoidmain(String[] args)
{
System.out.println("Hi, please type in a string.");
Scannerin = newScanner(System.in);
StringuserWord = in.nextLine();
System.out.println(userWord);
StringuserLower = userWord.toLowerCase();
System.out.println(userLower);
String[] userConverted = convertToArray(userLower);
booleanisDoubloon = isDoubloon(userConverted);
//System.out.println(Arrays.deepToString(isDoubloon));
System.out.println(isDoubloon);
if (isDoubloon == true)
System.out.println("Ya, your word is a doubloon!");
else
System.out.println("Boo, you suck. Your words suck!");
}
publicstaticString[] convertToArray(Stringword)
{
String[] doubloon = newString[word.length()];
for(inti = 0; i < word.length(); i++)
doubloon[i] = Character.toString(word.charAt(i));
System.out.println(Arrays.toString(doubloon));
returndoubloon;
}
publicstaticbooleanisDoubloon(String[] doubloon)
{
int[] counts = newint[26];
for(inti = 0; i < doubloon.length; i++)
{
//counts[((doubloon[i].chartAt(i))-'a')]++;
for (intj = 0; j < doubloon[i].length(); j++)
counts[(doubloon[i].charAt(j)-'a')]++;
System.out.println(Arrays.toString(counts));
}
/*for(char letter : doubloon.toCharArray())
counts[letter-'a']++;*/
for(intcount : counts)
{
if(count != 0 && count != 2)
returnfalse;
//return false;
}
returntrue;
}
}
/*
https://stackoverflow.com/questions/5235401/split-string-into-array-of-character-strings
An efficient way of turning a String into an array of one-character Strings would be to do this:
String[] res = new String[str.length()];
for (int i = 0; i < str.length(); i++) {
res[i] = Character.toString(str.charAt(i));
}
However, this does not take account of the fact that a char in a String could actually represent half of a Unicode code-point. (If the code-point is not in the BMP.) To deal with that you need to iterate through the code points ... which is more complicated.
This approach will be faster than using String.split("clever regex"), and it will probably be faster than using Java 8+ streams. It is probable faster than this:
String[] res = new String[str.length()];
int 0 = 0;
for (char ch: str.toCharArray[]) {
res[i++] = Character.toString(ch);
}
because toCharArray has to copy the characters to a new array.*/