File tree 1 file changed +49
-0
lines changed
1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -94,3 +94,52 @@ var numDupDigitsAtMostN = function(n) {
94
94
95
95
return n - res
96
96
} ;
97
+
98
+ // another
99
+
100
+ /**
101
+ * @param {number } n
102
+ * @return {number }
103
+ */
104
+ var numDupDigitsAtMostN = function ( n ) {
105
+ return n - helper ( n )
106
+ function helper ( num ) {
107
+ let res = 0
108
+ const s = `${ num } `
109
+ const slen = s . length
110
+ for ( let len = 1 ; len < slen ; len ++ ) {
111
+ res += perm ( 10 , len ) - perm ( 9 , len - 1 )
112
+ }
113
+ const visited = Array ( 10 ) . fill ( null )
114
+ dfs ( s , 0 , visited )
115
+
116
+ return res
117
+ function dfs ( s , i , visited ) {
118
+ const n = s . length
119
+ if ( i === n ) {
120
+ res ++
121
+ return
122
+ }
123
+ for ( let d = 0 ; d <= 9 ; d ++ ) {
124
+ if ( d === 0 && i === 0 ) continue
125
+ if ( visited [ d ] ) continue
126
+ if ( d < + s [ i ] ) {
127
+ res += perm ( 10 - i - 1 , n - i - 1 )
128
+ } else if ( d === + s [ i ] ) {
129
+ visited [ d ] = 1
130
+ dfs ( s , i + 1 , visited )
131
+ visited [ d ] = 0
132
+ }
133
+ }
134
+ }
135
+ }
136
+
137
+ function perm ( m , n ) {
138
+ if ( n === 0 ) return 1
139
+ let res = 1
140
+ for ( let i = 0 ; i < n ; i ++ ) {
141
+ res *= m - i
142
+ }
143
+ return res
144
+ }
145
+ } ;
You can’t perform that action at this time.
0 commit comments