Advertisement
K_S_

Untitled

Nov 16th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. // Runtime: 1 ms, faster than 100.00% of Java online submissions for Letter Case Permutation.
  2. // Memory Usage: 38.1 MB, less than 96.00% of Java online submissions for Letter Case Permutation.
  3.  
  4. class Solution {
  5.     public List<String> letterCasePermutation(String S) {
  6.         char[] chars = S.toCharArray();
  7.         List<String> result = new ArrayList<>();
  8.         helper(chars, 0, result);
  9.         return result;
  10.     }
  11.  
  12.     private void helper(char[] chars, int i, List<String> result) {
  13.         if (i == chars.length) {
  14.             result.add(new String(chars));
  15.             return;
  16.         }
  17.  
  18.  
  19.         char ch = chars[i];
  20.         if (Character.isDigit(ch)) {
  21.             helper(chars, i + 1, result);
  22.         }
  23.         if (Character.isLetter(ch)) {
  24.             chars[i] = Character.toUpperCase(ch);
  25.             helper(chars, i + 1, result);
  26.             chars[i] = Character.toLowerCase(ch);
  27.             helper(chars, i + 1, result);
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement