@@ -37,14 +37,37 @@ public static boolean isAllCharactersUniqueAndInASCII(String str) {
37
37
return true ;
38
38
}
39
39
40
- private static boolean hasAllUniqueCharactersWhenStringContainsAllLowercase (String s ) {
40
+ /**
41
+ * Check whether the input string contains different individual characters, lowercase
42
+ * and in between 'a' and 'z'
43
+ *
44
+ * @param str Input string
45
+ * @return true if all characters are different from each other,lowercase and between 'a' and 'z', otherwise false.
46
+ */
47
+ public static boolean isAllCharactersUniqueAndLowercaseAndInAlphabet (String str ) {
48
+ if (str == null
49
+ || str .isEmpty ()
50
+ || str .length () > 26 ) {
51
+ return false ;
52
+ }
53
+
41
54
int checker = 0 ;
42
- for (int i = 0 ; i < s .length (); i ++) {
43
- int charValue = s .charAt (i ) - 'a' ;
44
- if ((checker & (1 << charValue )) > 0 ) {
55
+ int stringLength = str .length ();
56
+ for (int i = 0 ; i < stringLength ; i ++) {
57
+
58
+ char character = str .charAt (i );
59
+ if (!(character >= 'a' && character <= 'z' )) {
45
60
return false ;
46
61
}
47
- checker |= (1 << charValue );
62
+
63
+ int characterIndex = character - 'a' ;
64
+ int singleBitOnPosition = 1 << characterIndex ;
65
+
66
+ if ((checker & singleBitOnPosition ) > 0 ) {
67
+ return false ;
68
+ }
69
+
70
+ checker |= singleBitOnPosition ; // checker = checker | singleBitOnPosition;
48
71
}
49
72
return true ;
50
73
}
@@ -58,11 +81,11 @@ public static void main(String[] args) {
58
81
System .out .println (isAllCharactersUniqueAndInASCII (s ));
59
82
System .out .println ("-------" );
60
83
s = "ram" ;
61
- System .out .println (hasAllUniqueCharactersWhenStringContainsAllLowercase (s ));
84
+ System .out .println (isAllCharactersUniqueAndLowercaseAndInAlphabet (s ));
62
85
s = "rama" ;
63
- System .out .println (hasAllUniqueCharactersWhenStringContainsAllLowercase (s ));
86
+ System .out .println (isAllCharactersUniqueAndLowercaseAndInAlphabet (s ));
64
87
// not working as the input contains different cases
65
88
s = "ramA" ;
66
- System .out .println (hasAllUniqueCharactersWhenStringContainsAllLowercase (s ));
89
+ System .out .println (isAllCharactersUniqueAndLowercaseAndInAlphabet (s ));
67
90
}
68
91
}
0 commit comments